2 * This file is part of the coreboot project.
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
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.
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.
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
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>
29 #include <cpu/x86/lapic.h>
32 #include "sb600_smbus.c"
36 #define MAINBOARD_POWER_OFF 0
37 #define MAINBOARD_POWER_ON 1
39 #ifndef MAINBOARD_POWER_ON_AFTER_POWER_FAIL
40 #define MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON
45 unsigned int value_low, value_high;
48 static struct ioapicreg ioapicregvalues[] = {
49 #define ALL (0xff << 24)
51 #define DISABLED (1 << 16)
52 #define ENABLED (0 << 16)
53 #define TRIGGER_EDGE (0 << 15)
54 #define TRIGGER_LEVEL (1 << 15)
55 #define POLARITY_HIGH (0 << 13)
56 #define POLARITY_LOW (1 << 13)
57 #define PHYSICAL_DEST (0 << 11)
58 #define LOGICAL_DEST (1 << 11)
59 #define ExtINT (7 << 8)
63 /* IO-APIC virtual wire mode configuration */
64 /* mask, trigger, polarity, destination, delivery, vector */
89 /* Be careful and don't write past the end... */
92 static void setup_ioapic(unsigned long ioapic_base)
95 unsigned long value_low, value_high;
96 volatile unsigned long *l;
97 struct ioapicreg *a = ioapicregvalues;
99 ioapicregvalues[0].value_high = lapicid() << (56 - 32);
101 printk_debug("lapicid = %016x\n", ioapicregvalues[0].value_high);
103 l = (unsigned long *)ioapic_base;
105 for (i = 0; i < ARRAY_SIZE(ioapicregvalues);
107 l[0] = (a->reg * 2) + 0x10;
110 l[0] = (a->reg * 2) + 0x11;
111 l[4] = a->value_high;
113 if ((i == 0) && (value_low == 0xffffffff)) {
114 printk_warning("IO APIC not responding.\n");
121 * SB600 enables all USB controllers by default in SMBUS Control.
122 * SB600 enables SATA by default in SMBUS Control.
124 static void sm_init(device_t dev)
129 unsigned long ioapic_base;
133 printk_info("sm_init().\n");
135 ioapic_base = pci_read_config32(dev, 0x74) & (0xffffffe0); /* some like mem resource, but does not have enable bit */
136 setup_ioapic(ioapic_base);
138 dword = pci_read_config8(dev, 0x62);
140 pci_write_config8(dev, 0x62, dword);
142 dword = pci_read_config32(dev, 0x78);
144 pci_write_config32(dev, 0x78, dword); /* enable 0xCD6 0xCD7 */
146 /* enable serial irq */
147 byte = pci_read_config8(dev, 0x69);
148 byte |= 1 << 7; /* enable serial irq function */
150 byte |= 4 << 2; /* set NumSerIrqBits=4 */
151 pci_write_config8(dev, 0x69, byte);
153 byte = pm_ioread(0x61);
154 byte |= 1 << 1; /* Set to enable NB/SB handshake during IOAPIC interrupt for AMD K8/K7 */
155 pm_iowrite(0x61, byte);
158 byte = pm_ioread(0x53);
160 pm_iowrite(0x53, byte);
162 /* power after power fail */
163 on = MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
164 get_option(&on, "power_on_after_fail");
165 byte = pm_ioread(0x74);
171 pm_iowrite(0x74, byte);
172 printk_info("set power %s after power fail\n", on ? "on" : "off");
174 /* sb600 rpr:2.3.3: */
175 byte = pm_ioread(0x9A);
176 byte |= 1 << 5 | 1 << 4 | 1 << 2;
177 pm_iowrite(0x9A, byte);
179 byte = pm_ioread(0x8F);
182 pm_iowrite(0x8F, byte);
184 pm_iowrite(0x8B, 0x01);
185 pm_iowrite(0x8A, 0x90);
186 pm_iowrite(0x88, 0x10); /* A21 */
188 byte = pm_ioread(0x7C);
190 pm_iowrite(0x7C, byte);
192 byte = pm_ioread(0x68);
194 pm_iowrite(0x68, byte);
196 byte = pm_ioread(0x8D);
198 pm_iowrite(0x8D, byte);
200 byte = pm_ioread(0x61);
202 pm_iowrite(0x61, byte);
204 byte = pm_ioread(0x42);
206 pm_iowrite(0x42, byte);
208 /* Set up NMI on errors */
209 byte = inb(0x70); /* RTC70 */
211 nmi_option = NMI_OFF;
212 get_option(&nmi_option, "nmi");
214 byte &= ~(1 << 7); /* set NMI */
215 printk_info("++++++++++set NMI+++++\n");
217 byte |= (1 << 7); /* Can not mask NMI from PCI-E and NMI_NOW */
218 printk_info("++++++++++no set NMI+++++\n");
221 if (byte != byte_old) {
225 /* 2.10 IO Trap Settings */
226 abcfg_reg(0x10090, 1 << 16, 1 << 16);
229 pci_write_config32(dev, 0xF0, AB_INDX);
230 /* Initialize the real time clock */
233 /*3.4 Enabling IDE/PCIB Prefetch for Performance Enhancement */
234 abcfg_reg(0x10060, 9 << 17, 9 << 17);
235 abcfg_reg(0x10064, 9 << 17, 9 << 17);
237 /* 3.5 Enabling OHCI Prefetch for Performance Enhancement */
238 abcfg_reg(0x80, 1 << 0, 1<< 0);
240 /* 3.6 B-Link Client's Credit Variable Settings for the Downstream Arbitration Equation */
241 /* 3.7 Enabling Additional Address Bits Checking in Downstream */
242 abcfg_reg(0x9c, 3 << 0, 3 << 0);
244 /* 3.8 Set B-Link Prefetch Mode */
245 abcfg_reg(0x80, 3 << 17, 3 << 17);
247 /* 3.9 Enabling Detection of Upstream Interrupts */
248 abcfg_reg(0x94, 1 << 20,1 << 20);
250 /* 3.10: Enabling Downstream Posted Transactions to Pass Non-Posted
251 * Transactions for the K8 Platform (for All Revisions) */
252 abcfg_reg(0x10090, 1 << 8, 1 << 8);
254 /* 3.11:Programming Cycle Delay for AB and BIF Clock Gating */
255 /* 3.12: Enabling AB and BIF Clock Gating */
256 abcfg_reg(0x10054, 0xFFFF0000, 0x1040000);
257 abcfg_reg(0x54, 0xFF << 16, 4 << 16);
258 printk_info("3.11, ABCFG:0x54\n");
259 abcfg_reg(0x54, 1 << 24, 1 << 24);
260 printk_info("3.12, ABCFG:0x54\n");
261 abcfg_reg(0x98, 0x0000FF00, 0x00004700);
263 /* 3.13:Enabling AB Int_Arbiter Enhancement (for All Revisions) */
264 abcfg_reg(0x10054, 0x0000FFFF, 0x07FF);
266 /* 3.14:Enabling L1 on A-link Express */
267 axcfg_reg(0x68, 0x00000003, 0x2);
268 axindxp_reg(0xa0, 0x0000F000, 0x6000);
270 abcfg_reg(0x10098, 0xFFFFFFFF, 0x4000);
271 abcfg_reg(0x04, 0xFFFFFFFF, 0x6);
272 printk_info("sm_init() end\n");
274 /* Enable NbSb virtual channel */
275 axcfg_reg(0x114, 0x3f << 1, 0 << 1);
276 axcfg_reg(0x120, 0x7f << 1, 0x7f << 1);
277 axcfg_reg(0x120, 7 << 24, 1 << 24);
278 axcfg_reg(0x120, 1 << 31, 1 << 31);
279 abcfg_reg(0x50, 1 << 3, 1 << 3);
282 static int lsmbus_recv_byte(device_t dev)
285 struct resource *res;
288 device = dev->path.u.i2c.device;
289 pbus = get_pbus_smbus(dev);
291 res = find_resource(pbus->dev, 0x10);
293 return do_smbus_recv_byte(res->base, device);
296 static int lsmbus_send_byte(device_t dev, u8 val)
299 struct resource *res;
302 device = dev->path.u.i2c.device;
303 pbus = get_pbus_smbus(dev);
305 res = find_resource(pbus->dev, 0x10);
307 return do_smbus_send_byte(res->base, device, val);
310 static int lsmbus_read_byte(device_t dev, u8 address)
313 struct resource *res;
316 device = dev->path.u.i2c.device;
317 pbus = get_pbus_smbus(dev);
319 res = find_resource(pbus->dev, 0x10);
321 return do_smbus_read_byte(res->base, device, address);
324 static int lsmbus_write_byte(device_t dev, u8 address, u8 val)
327 struct resource *res;
330 device = dev->path.u.i2c.device;
331 pbus = get_pbus_smbus(dev);
333 res = find_resource(pbus->dev, 0x10);
335 return do_smbus_write_byte(res->base, device, address, val);
337 static struct smbus_bus_operations lops_smbus_bus = {
338 .recv_byte = lsmbus_recv_byte,
339 .send_byte = lsmbus_send_byte,
340 .read_byte = lsmbus_read_byte,
341 .write_byte = lsmbus_write_byte,
344 static void sb600_sm_read_resources(device_t dev)
346 struct resource *res;
348 /* Get the normal pci resources of this device */
349 pci_dev_read_resources(dev);
352 res = new_resource(dev, 0x74);
353 res->base = 0xfec00000;
354 res->size = 256 * 0x10;
355 res->limit = 0xFFFFFFFFUL; /* res->base + res->size -1; */
358 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED;
360 /* dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER; */
362 compact_resources(dev);
365 static void sb600_sm_set_resources(struct device *dev)
367 struct resource *res;
369 pci_dev_set_resources(dev);
371 res = find_resource(dev, 0x74);
372 pci_write_config32(dev, 0x74, res->base | 1 << 3);
375 static struct pci_operations lops_pci = {
376 .set_subsystem = pci_dev_set_subsystem,
378 static struct device_operations smbus_ops = {
379 .read_resources = sb600_sm_read_resources,
380 .set_resources = sb600_sm_set_resources,
381 .enable_resources = pci_dev_enable_resources,
383 .scan_bus = scan_static_bus,
384 /* .enable = sb600_enable, */
385 .ops_pci = &lops_pci,
386 .ops_smbus_bus = &lops_smbus_bus,
388 static struct pci_driver smbus_driver __pci_driver = {
390 .vendor = PCI_VENDOR_ID_ATI,
391 .device = PCI_DEVICE_ID_ATI_SB600_SM,