sb600 has problems with the virtual wire mode setup in setup_ioapic(). It causes...
[coreboot.git] / src / southbridge / amd / sb600 / sb600_sm.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 <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 "sb600.h"
33 #include "sb600_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 * SB600 enables all USB controllers by default in SMBUS Control.
46 * SB600 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_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         clear_ioapic(ioapic_base);
62
63         dword = pci_read_config8(dev, 0x62);
64         dword |= 1 << 2;
65         pci_write_config8(dev, 0x62, dword);
66
67         dword = pci_read_config32(dev, 0x78);
68         dword |= 1 << 9;
69         pci_write_config32(dev, 0x78, dword);   /* enable 0xCD6 0xCD7 */
70
71         /* bit 10: MultiMediaTimerIrqEn */
72         dword = pci_read_config8(dev, 0x64);
73         dword |= 1 << 10;
74         pci_write_config8(dev, 0x64, dword);
75         /* enable serial irq */
76         byte = pci_read_config8(dev, 0x69);
77         byte |= 1 << 7;         /* enable serial irq function */
78         byte &= ~(0xF << 2);
79         byte |= 4 << 2;         /* set NumSerIrqBits=4 */
80         pci_write_config8(dev, 0x69, byte);
81
82         byte = pm_ioread(0x61);
83         byte |= 1 << 1;         /* Set to enable NB/SB handshake during IOAPIC interrupt for AMD K8/K7 */
84         pm_iowrite(0x61, byte);
85
86         /* disable SMI */
87         byte = pm_ioread(0x53);
88         byte |= 1 << 3;
89         pm_iowrite(0x53, byte);
90
91         /* power after power fail */
92         on = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
93         get_option(&on, "power_on_after_fail");
94         byte = pm_ioread(0x74);
95         byte &= ~0x03;
96         if (on) {
97                 byte |= 2;
98         }
99         byte |= 1 << 2;
100         pm_iowrite(0x74, byte);
101         printk_info("set power %s after power fail\n", on ? "on" : "off");
102
103         /* sb600 rpr:2.3.3: */
104         byte = pm_ioread(0x9A);
105         byte |= 1 << 5 | 1 << 4 | 1 << 2;
106         pm_iowrite(0x9A, byte);
107
108         byte = pm_ioread(0x8F);
109         byte |= 1 << 5;
110         byte &= ~(1 << 4);
111         pm_iowrite(0x8F, byte);
112
113         pm_iowrite(0x8B, 0x01);
114         pm_iowrite(0x8A, 0x90);
115         pm_iowrite(0x88, 0x10); /* A21 */
116
117         byte = pm_ioread(0x7C);
118         byte |= 1 << 0;
119         pm_iowrite(0x7C, byte);
120
121         byte = pm_ioread(0x68);
122         byte &= ~(1 << 1);
123         /* 2.6 */
124         byte |= 1 << 2;
125         pm_iowrite(0x68, byte);
126
127         /* 2.6 */
128         byte = pm_ioread(0x65);
129         byte &= ~(1 << 7);
130         pm_iowrite(0x65, byte);
131
132         /* 2.3.4 */
133         byte = pm_ioread(0x52);
134         byte &= ~0x2F;
135         byte |= 0x8;
136         pm_iowrite(0x52, byte);
137
138         byte = pm_ioread(0x8D);
139         byte &= ~(1 << 6);
140         pm_iowrite(0x8D, byte);
141
142         byte = pm_ioread(0x61);
143         byte &= ~(1 << 2);
144         pm_iowrite(0x61, byte);
145
146         byte = pm_ioread(0x42);
147         byte &= ~(1 << 2);
148         pm_iowrite(0x42, byte);
149
150         /* Set up NMI on errors */
151         byte = inb(0x70);       /* RTC70 */
152         byte_old = byte;
153         nmi_option = NMI_OFF;
154         get_option(&nmi_option, "nmi");
155         if (nmi_option) {
156                 byte &= ~(1 << 7);      /* set NMI */
157                 printk_info("++++++++++set NMI+++++\n");
158         } else {
159                 byte |= (1 << 7);       /* Can not mask NMI from PCI-E and NMI_NOW */
160                 printk_info("++++++++++no set NMI+++++\n");
161         }
162         byte &= ~(1 << 7);
163         if (byte != byte_old) {
164                 outb(byte, 0x70);
165         }
166
167         /* 2.10 IO Trap Settings */
168         abcfg_reg(0x10090, 1 << 16, 1 << 16);
169
170         /* ab index */
171         pci_write_config32(dev, 0xF0, AB_INDX);
172         /* Initialize the real time clock */
173         rtc_init(0);
174
175         /*3.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         /* 3.5 Enabling OHCI Prefetch for Performance Enhancement */
180         abcfg_reg(0x80, 1 << 0, 1<< 0);
181
182         /* 3.6 B-Link Client's Credit Variable Settings for the Downstream Arbitration Equation */
183         /* 3.7 Enabling Additional Address Bits Checking in Downstream */
184         abcfg_reg(0x9c, 3 << 0, 3 << 0);
185
186         /* 3.8 Set B-Link Prefetch Mode */
187         abcfg_reg(0x80, 3 << 17, 3 << 17);
188
189         /* 3.9 Enabling Detection of Upstream Interrupts */
190         abcfg_reg(0x94, 1 << 20,1 << 20);
191
192         /* 3.10: Enabling Downstream Posted Transactions to Pass Non-Posted
193          *  Transactions for the K8 Platform (for All Revisions) */
194         abcfg_reg(0x10090, 1 << 8, 1 << 8);
195
196         /* 3.11:Programming Cycle Delay for AB and BIF Clock Gating */
197         /* 3.12: Enabling AB and BIF Clock Gating */
198         abcfg_reg(0x10054, 0xFFFF0000, 0x1040000);
199         abcfg_reg(0x54, 0xFF << 16, 4 << 16);
200         printk_info("3.11, ABCFG:0x54\n");
201         abcfg_reg(0x54, 1 << 24, 1 << 24);
202         printk_info("3.12, ABCFG:0x54\n");
203         abcfg_reg(0x98, 0x0000FF00, 0x00004700);
204
205         /* 3.13:Enabling AB Int_Arbiter Enhancement (for All Revisions) */
206         abcfg_reg(0x10054, 0x0000FFFF, 0x07FF);
207
208         /* 3.14:Enabling L1 on A-link Express */
209         axcfg_reg(0x68, 0x00000003, 0x2);
210         axindxp_reg(0xa0, 0x0000F000, 0x6000);
211
212         abcfg_reg(0x10098, 0xFFFFFFFF, 0x4000);
213         abcfg_reg(0x04, 0xFFFFFFFF, 0x6);
214         printk_info("sm_init() end\n");
215
216         /* Enable NbSb virtual channel */
217         axcfg_reg(0x114, 0x3f << 1, 0 << 1);
218         axcfg_reg(0x120, 0x7f << 1, 0x7f << 1);
219         axcfg_reg(0x120, 7 << 24, 1 << 24);
220         axcfg_reg(0x120, 1 << 31, 1 << 31);
221         abcfg_reg(0x50, 1 << 3, 1 << 3);
222 }
223
224 static int lsmbus_recv_byte(device_t dev)
225 {
226         u32 device;
227         struct resource *res;
228         struct bus *pbus;
229
230         device = dev->path.i2c.device;
231         pbus = get_pbus_smbus(dev);
232
233         res = find_resource(pbus->dev, 0x10);
234
235         return do_smbus_recv_byte(res->base, device);
236 }
237
238 static int lsmbus_send_byte(device_t dev, u8 val)
239 {
240         u32 device;
241         struct resource *res;
242         struct bus *pbus;
243
244         device = dev->path.i2c.device;
245         pbus = get_pbus_smbus(dev);
246
247         res = find_resource(pbus->dev, 0x10);
248
249         return do_smbus_send_byte(res->base, device, val);
250 }
251
252 static int lsmbus_read_byte(device_t dev, u8 address)
253 {
254         u32 device;
255         struct resource *res;
256         struct bus *pbus;
257
258         device = dev->path.i2c.device;
259         pbus = get_pbus_smbus(dev);
260
261         res = find_resource(pbus->dev, 0x10);
262
263         return do_smbus_read_byte(res->base, device, address);
264 }
265
266 static int lsmbus_write_byte(device_t dev, u8 address, u8 val)
267 {
268         u32 device;
269         struct resource *res;
270         struct bus *pbus;
271
272         device = dev->path.i2c.device;
273         pbus = get_pbus_smbus(dev);
274
275         res = find_resource(pbus->dev, 0x10);
276
277         return do_smbus_write_byte(res->base, device, address, val);
278 }
279 static struct smbus_bus_operations lops_smbus_bus = {
280         .recv_byte = lsmbus_recv_byte,
281         .send_byte = lsmbus_send_byte,
282         .read_byte = lsmbus_read_byte,
283         .write_byte = lsmbus_write_byte,
284 };
285
286 static void sb600_sm_read_resources(device_t dev)
287 {
288         struct resource *res;
289         u8 byte;
290
291         /* rpr2.14: Hides SM bus controller Bar1 where stores HPET MMIO base address */
292         byte = pm_ioread(0x55);
293         byte |= 1 << 7;
294         pm_iowrite(0x55, byte);
295
296         /* Get the normal pci resources of this device */
297         /* pci_dev_read_resources(dev); */
298
299         byte = pm_ioread(0x55);
300         byte &= ~(1 << 7);
301         pm_iowrite(0x55, byte);
302
303         /* apic */
304         res = new_resource(dev, 0x74);
305         res->base  = 0xfec00000;
306         res->size = 256 * 0x10;
307         res->limit = 0xFFFFFFFFUL;      /* res->base + res->size -1; */
308         res->align = 8;
309         res->gran = 8;
310         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED;
311
312         res = new_resource(dev, 0x14); /* hpet */
313         res->base  = 0xfed00000;        /* reset hpet to widely accepted address */
314         res->size = 0x400;
315         res->limit = 0xFFFFFFFFUL;      /* res->base + res->size -1; */
316         res->align = 8;
317         res->gran = 8;
318         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED;
319         /* dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER; */
320
321         /* smbus */
322         res = new_resource(dev, 0x10);
323         res->base  = 0xB00;
324         res->size = 0x10;
325         res->limit = 0xFFFFUL;  /* res->base + res->size -1; */
326         res->align = 8;
327         res->gran = 8;
328         res->flags = IORESOURCE_IO | IORESOURCE_FIXED;
329
330
331         compact_resources(dev);
332
333 }
334 static void sb600_sm_set_resources(struct device *dev)
335 {
336         struct resource *res;
337         u8 byte;
338
339         pci_dev_set_resources(dev);
340
341
342         /* rpr2.14: Make HPET MMIO decoding controlled by the memory enable bit in command register of LPC ISA bridage */
343         byte = pm_ioread(0x52);
344         byte |= 1 << 6;
345         pm_iowrite(0x52, byte);
346
347         res = find_resource(dev, 0x74);
348         pci_write_config32(dev, 0x74, res->base | 1 << 3);
349
350         res = find_resource(dev, 0x14);
351         pci_write_config32(dev, 0x14, res->base);
352
353         res = find_resource(dev, 0x10);
354         pci_write_config32(dev, 0x10, res->base | 1);
355 }
356
357 static struct pci_operations lops_pci = {
358         .set_subsystem = pci_dev_set_subsystem,
359 };
360 static struct device_operations smbus_ops = {
361         .read_resources = sb600_sm_read_resources,
362         .set_resources = sb600_sm_set_resources,
363         .enable_resources = pci_dev_enable_resources,
364         .init = sm_init,
365         .scan_bus = scan_static_bus,
366         /* .enable           = sb600_enable, */
367         .ops_pci = &lops_pci,
368         .ops_smbus_bus = &lops_smbus_bus,
369 };
370 static struct pci_driver smbus_driver __pci_driver = {
371         .ops = &smbus_ops,
372         .vendor = PCI_VENDOR_ID_ATI,
373         .device = PCI_DEVICE_ID_ATI_SB600_SM,
374 };