SP5100's code is based on SB700. Change the legacy sb700 of sb7xx_51xx.
[coreboot.git] / src / southbridge / amd / 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 "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         u8 rev;
53         u32 dword;
54         u32 ioapic_base;
55         u32 on;
56         u32 nmi_option;
57
58         printk(BIOS_INFO, "sm_init().\n");
59
60         rev = get_sb700_revision(dev);
61         ioapic_base = pci_read_config32(dev, 0x74) & (0xffffffe0);      /* some like mem resource, but does not have  enable bit */
62         /* Don't rename APIC ID */
63         /* TODO: We should call setup_ioapic() here. But kernel hangs if cpu is K8.
64          * We need to check out why and change back. */
65         clear_ioapic(ioapic_base);
66
67         /* 2.10 Interrupt Routing/Filtering */
68         dword = pci_read_config8(dev, 0x62);
69         dword |= 3;
70         pci_write_config8(dev, 0x62, dword);
71
72         /* Delay back to back interrupts to the CPU. */
73         dword = pci_read_config16(dev, 0x64);
74         dword |= 1 << 13;
75         pci_write_config16(dev, 0x64, dword);
76
77         /* rrg:K8 INTR Enable (BIOS should set this bit after PIC initialization) */
78         /* rpr 2.1 Enabling Legacy Interrupt */
79         dword = pci_read_config8(dev, 0x62);
80         dword |= 1 << 2;
81         pci_write_config8(dev, 0x62, dword);
82
83         dword = pci_read_config32(dev, 0x78);
84         dword |= 1 << 9;
85         pci_write_config32(dev, 0x78, dword);   /* enable 0xCD6 0xCD7 */
86
87         /* bit 10: MultiMediaTimerIrqEn */
88         dword = pci_read_config8(dev, 0x64);
89         dword |= 1 << 10;
90         pci_write_config8(dev, 0x64, dword);
91         /* enable serial irq */
92         byte = pci_read_config8(dev, 0x69);
93         byte |= 1 << 7;         /* enable serial irq function */
94         byte &= ~(0xF << 2);
95         byte |= 4 << 2;         /* set NumSerIrqBits=4 */
96         pci_write_config8(dev, 0x69, byte);
97
98         /* IRQ0From8254 */
99         byte = pci_read_config8(dev, 0x41);
100         byte &= ~(1 << 7);
101         pci_write_config8(dev, 0x41, byte);
102
103         byte = pm_ioread(0x61);
104         byte |= 1 << 1;         /* Set to enable NB/SB handshake during IOAPIC interrupt for AMD K8/K7 */
105         pm_iowrite(0x61, byte);
106
107         /* disable SMI */
108         byte = pm_ioread(0x53);
109         byte |= 1 << 3;
110         pm_iowrite(0x53, byte);
111
112         /* power after power fail */
113         on = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
114         get_option(&on, "power_on_after_fail");
115         byte = pm_ioread(0x74);
116         byte &= ~0x03;
117         if (on) {
118                 byte |= 2;
119         }
120         byte |= 1 << 2;
121         pm_iowrite(0x74, byte);
122         printk(BIOS_INFO, "set power %s after power fail\n", on ? "on" : "off");
123
124         byte = pm_ioread(0x68);
125         byte &= ~(1 << 1);
126         /* 2.7 */
127         byte |= 1 << 2;
128         pm_iowrite(0x68, byte);
129
130         /* 2.7 */
131         byte = pm_ioread(0x65);
132         byte &= ~(1 << 7);
133         pm_iowrite(0x65, byte);
134
135         /* 2.16 */
136         byte = pm_ioread(0x55);
137         byte |= 1 << 5;
138         pm_iowrite(0x55, byte);
139
140         byte = pm_ioread(0xD7);
141         byte |= 1 << 6 | 1 << 1;;
142         pm_iowrite(0xD7, byte);
143
144         /* 2.15 */
145         byte = pm_ioread(0x42);
146         byte &= ~(1 << 2);
147         pm_iowrite(0x42, byte);
148
149         /* Set up NMI on errors */
150         byte = inb(0x70);       /* RTC70 */
151         byte_old = byte;
152         nmi_option = NMI_OFF;
153         get_option(&nmi_option, "nmi");
154         if (nmi_option) {
155                 byte &= ~(1 << 7);      /* set NMI */
156                 printk(BIOS_INFO, "++++++++++set NMI+++++\n");
157         } else {
158                 byte |= (1 << 7);       /* Can not mask NMI from PCI-E and NMI_NOW */
159                 printk(BIOS_INFO, "++++++++++no set NMI+++++\n");
160         }
161         byte &= ~(1 << 7);
162         if (byte != byte_old) {
163                 outb(byte, 0x70);
164         }
165
166         /*rpr v2.13  2.22 SMBUS PCI Config */
167         byte = pci_read_config8(dev, 0xE1);
168         if ((REV_SB700_A11 == rev) || REV_SB700_A12 == rev) {
169                 byte |= 1 << 0;
170         }
171         /*Set bit2 to 1, enable Io port 60h read/wrire SMi trapping and
172          *Io port 64h write Smi trapping. conflict with ps2 keyboard
173          */
174         //byte |= 1 << 2 | 1 << 3 | 1 << 4;
175         byte |= 1 << 3 | 1 << 4;
176         pci_write_config8(dev, 0xE1, byte);
177
178         /* 2.5 Enabling Non-Posted Memory Write */
179         axindxc_reg(0x10, 1 << 9, 1 << 9);
180
181         /* 2.11 IO Trap Settings */
182         abcfg_reg(0x10090, 1 << 16, 1 << 16);
183
184         /* ab index */
185         pci_write_config32(dev, 0xF0, AB_INDX);
186         /* Initialize the real time clock */
187         rtc_init(0);
188
189         /* 4.3 Enabling Upstream DMA Access */
190         axcfg_reg(0x04, 1 << 2, 1 << 2);
191         /* 4.4 Enabling IDE/PCIB Prefetch for Performance Enhancement */
192         abcfg_reg(0x10060, 9 << 17, 9 << 17);
193         abcfg_reg(0x10064, 9 << 17, 9 << 17);
194
195         /* 4.5 Enabling OHCI Prefetch for Performance Enhancement, A12 */
196         abcfg_reg(0x80, 1 << 0, 1<< 0);
197
198         /* 4.6 B-Link Client's Credit Variable Settings for the Downstream Arbitration Equation */
199         /* 4.7 Enabling Additional Address Bits Checking in Downstream */
200         /* 4.16 IO write and SMI ordering enhancement*/
201         abcfg_reg(0x9c, 3 << 0, 3 << 0);
202         if (REV_SB700_A12 == rev) {
203                 abcfg_reg(0x9c, 1 << 8, 1 << 8);
204         } else if (rev >= REV_SB700_A14) {
205                 abcfg_reg(0x9c, 1 << 8, 0 << 8);
206         }
207         if (REV_SB700_A15 == rev) {
208                 abcfg_reg(0x90, 1 << 21, 1 << 21);
209                 abcfg_reg(0x9c, 1 << 5 | 1 << 9 | 1 << 15, 1 << 5 | 1 << 9 | 1 << 15);
210         }
211
212         /* 4.8 Set B-Link Prefetch Mode */
213         abcfg_reg(0x80, 3 << 17, 3 << 17);
214
215         /* 4.9 Enabling Detection of Upstream Interrupts */
216         abcfg_reg(0x94, 1 << 20 | 0x7FFFF, 1 << 20 | 0x00FEE);
217
218         /* 4.10: Enabling Downstream Posted Transactions to Pass Non-Posted
219          *  Transactions for the K8 Platform (for All Revisions) */
220         abcfg_reg(0x10090, 1 << 8, 1 << 8);
221
222         /* ACPI_SOFT_CLOCK_THROTTLE_PERIOD */
223         byte = pm_ioread(0x68);
224         byte &= ~(3 << 6);
225         byte |= (2 << 6);       /* 224us */
226         pm_iowrite(0x68, byte);
227
228         if (REV_SB700_A15 == rev) {
229                 u16 word;
230
231                 /* rpr v2.13 4.18 Enabling Posted Pass Non-Posted Downstream */
232                 axindxc_reg(0x02, 1 << 9, 1 << 9);
233                 abcfg_reg(0x9C, 0x00007CC0, 0x00007CC0);
234                 abcfg_reg(0x1009C, 0x00000030, 0x00000030);
235                 abcfg_reg(0x10090, 0x00001E00, 0x00001E00);
236
237                 /* rpr v2.13 4.19 Enabling Posted Pass Non-Posted Upstream */
238                 abcfg_reg(0x58, 0x0000F800, 0x0000E800);
239
240                 /* rpr v2.13 4.20 64 bit Non-Posted Memory Write Support */
241                 axindxc_reg(0x02, 1 << 10, 1 << 10);
242
243                 /* rpr v2.13 2.38 Unconditional Shutdown */
244                 byte = pci_read_config8(dev, 0x43);
245                 byte &= ~(1 << 3);
246                 pci_write_config8(dev, 0x43, byte);
247
248                 word = pci_read_config16(dev, 0x38);
249                 word |= 1 << 12;
250                 pci_write_config16(dev, 0x38, word);
251
252                 byte |= 1 << 3;
253                 pci_write_config8(dev, 0x43, byte);
254         }
255         //ACPI_DISABLE_TIMER_IRQ_ENHANCEMENT_FOR_8254_TIMER
256         byte = pci_read_config8(dev, 0xAE);
257         byte |= 1 << 5;
258         pci_write_config8(dev, 0xAE, byte);
259
260         /* 4.11:Programming Cycle Delay for AB and BIF Clock Gating */
261         /* 4.12: Enabling AB and BIF Clock Gating */
262         abcfg_reg(0x10054, 0xFFFF0000, 0x1040000);
263         abcfg_reg(0x54, 0xFF << 16, 4 << 16);
264         abcfg_reg(0x54, 1 << 24, 0 << 24);
265         abcfg_reg(0x98, 0x0000FF00, 0x00004700);
266
267         /* 4.13:Enabling AB Int_Arbiter Enhancement (for All Revisions) */
268         abcfg_reg(0x10054, 0x0000FFFF, 0x07FF);
269
270         /* 4.14:Enabling Requester ID for upstream traffic. */
271         abcfg_reg(0x98, 1 << 16, 1 << 16);
272
273         /* 9.2: Enabling IDE Data Bus DD7 Pull Down Resistor */
274         byte = pm2_ioread(0xE5);
275         byte |= 1 << 2;
276         pm2_iowrite(0xE5, byte);
277
278         /* Enable IDE controller. */
279         byte = pm_ioread(0x59);
280         byte &= ~(1 << 1);
281         pm_iowrite(0x59, byte);
282
283         printk(BIOS_INFO, "sm_init() end\n");
284
285         /* Enable NbSb virtual channel */
286         axcfg_reg(0x114, 0x3f << 1, 0 << 1);
287         axcfg_reg(0x120, 0x7f << 1, 0x7f << 1);
288         axcfg_reg(0x120, 7 << 24, 1 << 24);
289         axcfg_reg(0x120, 1 << 31, 1 << 31);
290         abcfg_reg(0x50, 1 << 3, 1 << 3);
291 }
292
293 static int lsmbus_recv_byte(device_t dev)
294 {
295         u32 device;
296         struct resource *res;
297         struct bus *pbus;
298
299         device = dev->path.i2c.device;
300         pbus = get_pbus_smbus(dev);
301
302         res = find_resource(pbus->dev, 0x90);
303
304         return do_smbus_recv_byte(res->base, device);
305 }
306
307 static int lsmbus_send_byte(device_t dev, u8 val)
308 {
309         u32 device;
310         struct resource *res;
311         struct bus *pbus;
312
313         device = dev->path.i2c.device;
314         pbus = get_pbus_smbus(dev);
315
316         res = find_resource(pbus->dev, 0x90);
317
318         return do_smbus_send_byte(res->base, device, val);
319 }
320
321 static int lsmbus_read_byte(device_t dev, u8 address)
322 {
323         u32 device;
324         struct resource *res;
325         struct bus *pbus;
326
327         device = dev->path.i2c.device;
328         pbus = get_pbus_smbus(dev);
329
330         res = find_resource(pbus->dev, 0x90);
331
332         return do_smbus_read_byte(res->base, device, address);
333 }
334
335 static int lsmbus_write_byte(device_t dev, u8 address, u8 val)
336 {
337         u32 device;
338         struct resource *res;
339         struct bus *pbus;
340
341         device = dev->path.i2c.device;
342         pbus = get_pbus_smbus(dev);
343
344         res = find_resource(pbus->dev, 0x90);
345
346         return do_smbus_write_byte(res->base, device, address, val);
347 }
348
349 static struct smbus_bus_operations lops_smbus_bus = {
350         .recv_byte = lsmbus_recv_byte,
351         .send_byte = lsmbus_send_byte,
352         .read_byte = lsmbus_read_byte,
353         .write_byte = lsmbus_write_byte,
354 };
355
356 static void sb700_sm_read_resources(device_t dev)
357 {
358         struct resource *res;
359
360         /* Get the normal pci resources of this device */
361         /* pci_dev_read_resources(dev); */
362
363         /* apic */
364         res = new_resource(dev, 0x74);
365         res->base  = IO_APIC_ADDR;
366         res->size = 256 * 0x10;
367         res->limit = 0xFFFFFFFFUL;      /* res->base + res->size -1; */
368         res->align = 8;
369         res->gran = 8;
370         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED;
371
372         /* Linux ACPI crashes when it is 1. For late debugging. */
373         res = new_resource(dev, 0xB4);  /* TODO: test hpet */
374         res->base  = 0xfed00000;        /* reset hpet to widely accepted address */
375         res->size = 0x400;
376         res->limit = 0xFFFFFFFFUL;      /* res->base + res->size -1; */
377         res->align = 8;
378         res->gran = 8;
379         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED;
380
381         /* dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER; */
382
383         /* smbus */
384         res = new_resource(dev, 0x90);
385         res->base  = 0xB00;
386         res->size = 0x10;
387         res->limit = 0xFFFFUL;  /* res->base + res->size -1; */
388         res->align = 8;
389         res->gran = 8;
390         res->flags = IORESOURCE_IO | IORESOURCE_FIXED;
391
392         compact_resources(dev);
393 }
394
395 static void sb700_sm_set_resources(struct device *dev)
396 {
397         struct resource *res;
398         u8 byte;
399
400         pci_dev_set_resources(dev);
401         res = find_resource(dev, 0x74);
402         pci_write_config32(dev, 0x74, res->base | 1 << 3);
403
404         /* TODO: test hpet */
405 #if 0   //rrg-2.0.3 shows BAR1 not used
406         /* Make SMBUS BAR1(HPET base at offset 14h) visible */
407         byte = pci_read_config8(dev, 0x43);
408         byte &= ~(1 << 3);
409         pci_write_config8(dev, 0x43, byte);
410 #endif
411
412         res = find_resource(dev, 0xB4);
413         /* Program HPET BAR Address */
414         pci_write_config32(dev, 0xB4, res->base);
415
416         /* Enable decoding of HPET MMIO, enable HPET MSI */
417         byte = pci_read_config8(dev, 0x43);
418         //byte |= (1 << 3); // Make SMBus Bar1 invisible
419         //byte |= ((1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
420         byte |= (1 << 4);
421         pci_write_config8(dev, 0x43, byte);
422
423         /* Enable HPET irq */
424         byte = pci_read_config8(dev, 0x65);
425         byte |= (1 << 2);
426         pci_write_config8(dev, 0x65, byte);
427         /* TODO: End of test hpet */
428
429         res = find_resource(dev, 0x90);
430         pci_write_config32(dev, 0x90, res->base | 1);
431 }
432
433 static struct pci_operations lops_pci = {
434         .set_subsystem = pci_dev_set_subsystem,
435 };
436
437 static struct device_operations smbus_ops = {
438         .read_resources = sb700_sm_read_resources,
439         .set_resources = sb700_sm_set_resources,
440         .enable_resources = pci_dev_enable_resources,
441         .init = sm_init,
442         .scan_bus = scan_static_bus,
443         .ops_pci = &lops_pci,
444         .ops_smbus_bus = &lops_smbus_bus,
445 };
446
447 static const struct pci_driver smbus_driver __pci_driver = {
448         .ops = &smbus_ops,
449         .vendor = PCI_VENDOR_ID_ATI,
450         .device = PCI_DEVICE_ID_ATI_SB700_SM,
451 };