This sb800 code is derived from sb700.
[coreboot.git] / src / southbridge / amd / sb800 / 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 "sb800.h"
33 #include "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 #define BIT0    (1 << 0)
45 #define BIT1    (1 << 1)
46 #define BIT2    (1 << 2)
47 #define BIT3    (1 << 3)
48 #define BIT4    (1 << 4)
49 #define BIT5    (1 << 5)
50 #define BIT6    (1 << 6)
51 #define BIT7    (1 << 7)
52
53 #define BIT8    (1 << 8 )
54 #define BIT9    (1 << 9 )
55 #define BIT10   (1 << 10)
56 #define BIT11   (1 << 11)
57 #define BIT12   (1 << 12)
58 #define BIT13   (1 << 13)
59 #define BIT14   (1 << 14)
60 #define BIT15   (1 << 15)
61
62 #define BIT16   (1 << 16)
63 #define BIT17   (1 << 17)
64 #define BIT18   (1 << 18)
65 #define BIT19   (1 << 19)
66 #define BIT20   (1 << 20)
67 #define BIT21   (1 << 21)
68 #define BIT22   (1 << 22)
69 #define BIT23   (1 << 23)
70 #define BIT24   (1 << 24)
71 #define BIT25   (1 << 25)
72 #define BIT26   (1 << 26)
73 #define BIT27   (1 << 27)
74 #define BIT28   (1 << 28)
75 #define BIT29   (1 << 29)
76 #define BIT30   (1 << 30)
77 #define BIT31   (1 << 31)
78
79 /*
80 * SB800 enables all USB controllers by default in SMBUS Control.
81 * SB800 enables SATA by default in SMBUS Control.
82 */
83
84 static void sm_init(device_t dev)
85 {
86         u8 byte;
87         u32 ioapic_base;
88
89         printk(BIOS_INFO, "sm_init().\n");
90
91         ioapic_base = 0xFEC00000;//pci_read_config32(dev, 0x74) & (0xffffffe0); /* some like mem resource, but does not have  enable bit */
92         /* Don't rename APIC ID */
93         /* TODO: We should call setup_ioapic() here. But kernel hangs if cpu is K8.
94          * We need to check out why and change back. */
95         clear_ioapic(ioapic_base);
96         //setup_ioapic(ioapic_base, 0);
97
98         /* enable serial irq */
99         byte = pm_ioread(0x54);
100         byte |= 1 << 7;         /* enable serial irq function */
101         byte &= ~(0xF << 2);
102         byte |= 4 << 2;         /* set NumSerIrqBits=4 */
103         pm_iowrite(0x54, byte);
104
105         pm_iowrite(0x00, 0x0E);
106         pm_iowrite(0x0B, 0x02);
107         /* 2.11 IO Trap Settings */
108         abcfg_reg(0x10090, 1 << 16, 1 << 16);
109
110         /* 4.1 ab index */
111         //pci_write_config32(dev, 0xF0, AB_INDX);
112         pm_iowrite(0xE0, AB_INDX & 0xFF);
113         pm_iowrite(0xE1, (AB_INDX >> 8) & 0xFF);
114         pm_iowrite(0xE2, (AB_INDX >> 16) & 0xFF);
115         pm_iowrite(0xE3, (AB_INDX >> 24) & 0xFF);
116         /* Initialize the real time clock */
117         rtc_init(0);
118
119         byte = pm_ioread(0x8);
120         byte |= 1 << 2 | 1 << 4;
121         pm_iowrite(0x08, byte);
122         byte = pm_ioread(0x9);
123         byte |= 1 << 0;
124         pm_iowrite(0x09, byte);
125
126         abcfg_reg(0x10060, (BIT31), BIT31);
127         abcfg_reg(0x1009C, (BIT4 + BIT5), BIT4 + BIT5);
128         abcfg_reg(0x9C,    (BIT2 + BIT3 + BIT4 + BIT5 + BIT6 + BIT7), BIT2 + BIT3 + BIT4 + BIT5 + BIT6 + BIT7);
129         abcfg_reg(0x90,    (BIT21 + BIT22 + BIT23), BIT21 + BIT22 + BIT23);
130         abcfg_reg(0xF0,    (BIT6 + BIT5), BIT6 + BIT5);
131         abcfg_reg(0x10090, (BIT9 + BIT10 + BIT11 + BIT12), BIT9 + BIT10 + BIT11 + BIT12);
132         abcfg_reg(0x58,    (BIT10), BIT10);
133         abcfg_reg(0xF0,    (BIT3 + BIT4), BIT3 + BIT4);
134         abcfg_reg(0x54,    (BIT1), BIT1);
135         //
136         axindxc_reg(0x02, BIT9, BIT9);
137         axindxc_reg(0x10, BIT9, BIT9);
138
139         /* 4.2 Enabling Upstream DMA Access */
140         axcfg_reg(0x04, 1 << 2, 1 << 2);
141         /* 4.3 Enabling PCIB Prefetch Settings */
142         abcfg_reg(0x10060, 1 << 20, 1 << 20);
143         abcfg_reg(0x10064, 1 << 20, 1 << 20);
144
145         /* 4.4 Enabling OHCI Prefetch for Performance Enhancement, A12 */
146         abcfg_reg(0x80, 1 << 0, 1<< 0);
147
148         /* 4.5 B-Link Client's Credit Variable Settings for the Downstream Arbitration Equation */
149         /* 4.6 Enabling Additional Address Bits Checking in Downstream */
150         abcfg_reg(0x9c, 1 << 0, 1 << 0);
151         //abcfg_reg(0x9c, 3 << 0, 3 << 0); //A11
152
153         /* 4.7 Set B-Link Prefetch Mode */
154         abcfg_reg(0x80, 3 << 17, 3 << 17);
155
156         // RPR Enabled SMI ordering enhancement. ABCFG 0x90[21]
157         // RPR USB Delay A-Link Express L1 State. ABCFG 0x90[17]
158         abcfg_reg(0x90, 1 << 17 | 1 << 21, 1 << 17 | 1 << 21);
159         /* 4.8 Enabling Detection of Upstream Interrupts */
160         abcfg_reg(0x94, 1 << 20 | 0x7FFFF, 1 << 20 | 0x00FEE);
161
162         /* 4.9: Enabling Downstream Posted Transactions to Pass Non-Posted
163          *  Transactions for the K8 Platform (for All Revisions) */
164         abcfg_reg(0x10090, 1 << 8, 1 << 8);
165
166         /* 4.10:Programming Cycle Delay for AB and BIF Clock Gating */
167         /* 4.11:Enabling AB Int_Arbiter Enhancement (for All Revisions) */
168         abcfg_reg(0x10054, 0xFFFF0000, 0x01040000);
169         abcfg_reg(0x54, 0xFF << 16, 4 << 16);
170         abcfg_reg(0x54, 1 << 24, 0 << 24);
171         abcfg_reg(0x54, 1 << 26, 1 << 26);
172         abcfg_reg(0x98, 0xFFFFFF00, 0x00004700);
173
174         /* 4.12: Enabling AB and BIF Clock Gating */
175         abcfg_reg(0x10054, 0x0000FFFF, 0x07FF);
176
177         /* 4.13:Enabling Requester ID for upstream traffic. */
178         abcfg_reg(0x98, 3 << 16, 3 << 16);
179
180         abcfg_reg(0x50, 1 << 2, 0 << 2);
181
182         /* 5.2 Enabling GPP Port A/B/C/D */
183         //abcfg_reg(0xC0, 0xF << 4, 0xF << 4);
184
185         /* Enable SCI as irq9. */
186         outb(0x10, 0xC00);
187         outb(0x9, 0xC01);
188         /* Enabled IRQ input */
189         outb(0x9, 0xC00);
190         outb(0xF7, 0xC01);
191
192         abcfg_reg(0x90, 0xFFFFFFFF, 0x00F80040);
193         abcfg_reg(0xA0, 0xFFFFFFFF, 0x00000000);
194         abcfg_reg(0xA4, 0xFFFFFFFF, 0x00000000);
195         abcfg_reg(0xC0, 0xFFFFFFFF, 0x0000F014);
196         abcfg_reg(0x98, 0xFFFFFFFF, 0X01034700);
197 }
198
199 static int lsmbus_recv_byte(device_t dev)
200 {
201         u32 device;
202         struct resource *res;
203         struct bus *pbus;
204
205         device = dev->path.i2c.device;
206         pbus = get_pbus_smbus(dev);
207
208         res = find_resource(pbus->dev, 0x90);
209
210         return do_smbus_recv_byte(res->base, device);
211 }
212
213 static int lsmbus_send_byte(device_t dev, u8 val)
214 {
215         u32 device;
216         struct resource *res;
217         struct bus *pbus;
218
219         device = dev->path.i2c.device;
220         pbus = get_pbus_smbus(dev);
221
222         res = find_resource(pbus->dev, 0x90);
223
224         return do_smbus_send_byte(res->base, device, val);
225 }
226
227 static int lsmbus_read_byte(device_t dev, u8 address)
228 {
229         u32 device;
230         struct resource *res;
231         struct bus *pbus;
232
233         device = dev->path.i2c.device;
234         pbus = get_pbus_smbus(dev);
235
236         res = find_resource(pbus->dev, 0x90);
237
238         return do_smbus_read_byte(res->base, device, address);
239 }
240
241 static int lsmbus_write_byte(device_t dev, u8 address, u8 val)
242 {
243         u32 device;
244         struct resource *res;
245         struct bus *pbus;
246
247         device = dev->path.i2c.device;
248         pbus = get_pbus_smbus(dev);
249
250         res = find_resource(pbus->dev, 0x90);
251
252         return do_smbus_write_byte(res->base, device, address, val);
253 }
254 static struct smbus_bus_operations lops_smbus_bus = {
255         .recv_byte = lsmbus_recv_byte,
256         .send_byte = lsmbus_send_byte,
257         .read_byte = lsmbus_read_byte,
258         .write_byte = lsmbus_write_byte,
259 };
260
261 static void sb800_sm_read_resources(device_t dev)
262 {
263         struct resource *res;
264         u8 byte;
265
266         /* rpr2.14: Hides SM bus controller Bar1 where stores HPET MMIO base address */
267         byte = pm_ioread(0x55);
268         byte |= 1 << 7;
269         pm_iowrite(0x55, byte);
270
271         /* Get the normal pci resources of this device */
272         /* pci_dev_read_resources(dev); */
273
274         byte = pm_ioread(0x55);
275         byte &= ~(1 << 7);
276         pm_iowrite(0x55, byte);
277
278         /* apic */
279         res = new_resource(dev, 0x74);
280         res->base  = 0xfec00000;
281         res->size = 256 * 0x10;
282         res->limit = 0xFEFFFFFUL;       /* res->base + res->size -1; */
283         res->align = 8;
284         res->gran = 8;
285         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED;
286
287         #if 0                          /* Linux ACPI crashes when it is 1. For late debugging. */
288         res = new_resource(dev, 0x14); /* TODO: hpet */
289         res->base  = 0xfed00000;        /* reset hpet to widely accepted address */
290         res->size = 0x400;
291         res->limit = 0xFFFFFFFFUL;      /* res->base + res->size -1; */
292         res->align = 8;
293         res->gran = 8;
294         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED;
295         #endif
296         /* dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER; */
297
298         /* smbus */
299         //res = new_resource(dev, 0x90);
300         //res->base  = 0xB00;
301         //res->size = 0x10;
302         //res->limit = 0xFFFFUL;        /* res->base + res->size -1; */
303         //res->align = 8;
304         //res->gran = 8;
305         //res->flags = IORESOURCE_IO | IORESOURCE_FIXED;
306
307
308         compact_resources(dev);
309 }
310
311 static void sb800_sm_set_resources(struct device *dev)
312 {
313         struct resource *res;
314         u8 byte;
315
316         pci_dev_set_resources(dev);
317
318
319         /* rpr2.14: Make HPET MMIO decoding controlled by the memory enable bit in command register of LPC ISA bridage */
320         byte = pm_ioread(0x52);
321         byte |= 1 << 6;
322         pm_iowrite(0x52, byte);
323
324         res = find_resource(dev, 0x74);
325
326         printk(BIOS_INFO, "sb800_sm_set_resources, res->base=0x%llx\n", res->base);
327
328         //pci_write_config32(dev, 0x74, res->base | 1 << 3);
329         pm_iowrite(0x34, res->base | 0x7);
330         pm_iowrite(0x35, (res->base >> 8) & 0xFF);
331         pm_iowrite(0x36, (res->base >> 16) & 0xFF);
332         pm_iowrite(0x37, (res->base >> 24) & 0xFF);
333 #if 0                           /* TODO:hpet */
334         res = find_resource(dev, 0x14);
335         pci_write_config32(dev, 0x14, res->base);
336 #endif
337         //res = find_resource(dev, 0x90);
338         //pci_write_config32(dev, 0x90, res->base | 1);
339 }
340
341 static struct pci_operations lops_pci = {
342         .set_subsystem = pci_dev_set_subsystem,
343 };
344 static struct device_operations smbus_ops = {
345         .read_resources = sb800_sm_read_resources,
346         .set_resources = sb800_sm_set_resources,
347         .enable_resources = pci_dev_enable_resources,
348         .init = sm_init,
349         .scan_bus = scan_static_bus,
350         .ops_pci = &lops_pci,
351         .ops_smbus_bus = &lops_smbus_bus,
352 };
353 static const struct pci_driver smbus_driver __pci_driver = {
354         .ops = &smbus_ops,
355         .vendor = PCI_VENDOR_ID_ATI,
356         .device = PCI_DEVICE_ID_ATI_SB800_SM,
357 };