The ARRAY_SIZE macro is convenient, yet mostly unused. Switch lots of
[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 <stdlib.h>
31 #include "sb600.h"
32 #include "sb600_smbus.c"
33
34 #define NMI_OFF 0
35
36 #define MAINBOARD_POWER_OFF 0
37 #define MAINBOARD_POWER_ON 1
38
39 #ifndef MAINBOARD_POWER_ON_AFTER_POWER_FAIL
40 #define MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON
41 #endif
42
43 struct ioapicreg {
44         unsigned int reg;
45         unsigned int value_low, value_high;
46 };
47
48 static struct ioapicreg ioapicregvalues[] = {
49 #define ALL             (0xff << 24)
50 #define NONE            (0)
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)
60 #define NMI             (4 << 8)
61 #define SMI             (2 << 8)
62 #define INT             (1 << 8)
63         /* IO-APIC virtual wire mode configuration */
64         /* mask, trigger, polarity, destination, delivery, vector */
65         {0, DISABLED, NONE},
66         {1, DISABLED, NONE},
67         {2, DISABLED, NONE},
68         {3, DISABLED, NONE},
69         {4, DISABLED, NONE},
70         {5, DISABLED, NONE},
71         {6, DISABLED, NONE},
72         {7, DISABLED, NONE},
73         {8, DISABLED, NONE},
74         {9, DISABLED, NONE},
75         {10, DISABLED, NONE},
76         {11, DISABLED, NONE},
77         {12, DISABLED, NONE},
78         {13, DISABLED, NONE},
79         {14, DISABLED, NONE},
80         {15, DISABLED, NONE},
81         {16, DISABLED, NONE},
82         {17, DISABLED, NONE},
83         {18, DISABLED, NONE},
84         {19, DISABLED, NONE},
85         {20, DISABLED, NONE},
86         {21, DISABLED, NONE},
87         {22, DISABLED, NONE},
88         {23, DISABLED, NONE},
89         /* Be careful and don't write past the end... */
90 };
91
92 static void setup_ioapic(unsigned long ioapic_base)
93 {
94         int i;
95         unsigned long value_low, value_high;
96         volatile unsigned long *l;
97         struct ioapicreg *a = ioapicregvalues;
98
99         ioapicregvalues[0].value_high = lapicid() << (56 - 32);
100
101         printk_debug("lapicid = %016x\n", ioapicregvalues[0].value_high);
102
103         l = (unsigned long *)ioapic_base;
104
105         for (i = 0; i < ARRAY_SIZE(ioapicregvalues);
106              i++, a++) {
107                 l[0] = (a->reg * 2) + 0x10;
108                 l[4] = a->value_low;
109                 value_low = l[4];
110                 l[0] = (a->reg * 2) + 0x11;
111                 l[4] = a->value_high;
112                 value_high = l[4];
113                 if ((i == 0) && (value_low == 0xffffffff)) {
114                         printk_warning("IO APIC not responding.\n");
115                         return;
116                 }
117         }
118 }
119
120 /*
121 * SB600 enables all USB controllers by default in SMBUS Control.
122 * SB600 enables SATA by default in SMBUS Control.
123 */
124 static void sm_init(device_t dev)
125 {
126         u8 byte;
127         u8 byte_old;
128         u32 dword;
129         unsigned long ioapic_base;
130         int on;
131         int nmi_option;
132
133         printk_info("sm_init().\n");
134
135         ioapic_base = pci_read_config32(dev, 0x74) & (0xffffffe0);      /* some like mem resource, but does not have  enable bit */
136         setup_ioapic(ioapic_base);
137
138         dword = pci_read_config8(dev, 0x62);
139         dword |= 1 << 2;
140         pci_write_config8(dev, 0x62, dword);
141
142         dword = pci_read_config32(dev, 0x78);
143         dword |= 1 << 9;
144         pci_write_config32(dev, 0x78, dword);   /* enable 0xCD6 0xCD7 */
145
146         /* enable serial irq */
147         byte = pci_read_config8(dev, 0x69);
148         byte |= 1 << 7;         /* enable serial irq function */
149         byte &= ~(0xF << 2);
150         byte |= 4 << 2;         /* set NumSerIrqBits=4 */
151         pci_write_config8(dev, 0x69, byte);
152
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);
156
157         /* disable SMI */
158         byte = pm_ioread(0x53);
159         byte |= 1 << 3;
160         pm_iowrite(0x53, byte);
161
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);
166         byte &= ~0x03;
167         if (on) {
168                 byte |= 2;
169         }
170         byte |= 1 << 2;
171         pm_iowrite(0x74, byte);
172         printk_info("set power %s after power fail\n", on ? "on" : "off");
173
174         /* sb600 rpr:2.3.3: */
175         byte = pm_ioread(0x9A);
176         byte |= 1 << 5 | 1 << 4 | 1 << 2;
177         pm_iowrite(0x9A, byte);
178
179         byte = pm_ioread(0x8F);
180         byte |= 1 << 5;
181         byte &= ~(1 << 4);
182         pm_iowrite(0x8F, byte);
183
184         pm_iowrite(0x8B, 0x01);
185         pm_iowrite(0x8A, 0x90);
186         pm_iowrite(0x88, 0x10); /* A21 */
187
188         byte = pm_ioread(0x7C);
189         byte |= 1 << 0;
190         pm_iowrite(0x7C, byte);
191
192         byte = pm_ioread(0x68);
193         byte &= ~(1 << 1);
194         pm_iowrite(0x68, byte);
195
196         byte = pm_ioread(0x8D);
197         byte &= ~(1 << 6);
198         pm_iowrite(0x8D, byte);
199
200         byte = pm_ioread(0x61);
201         byte &= ~(1 << 2);
202         pm_iowrite(0x61, byte);
203
204         byte = pm_ioread(0x42);
205         byte &= ~(1 << 2);
206         pm_iowrite(0x42, byte);
207
208         /* Set up NMI on errors */
209         byte = inb(0x70);       /* RTC70 */
210         byte_old = byte;
211         nmi_option = NMI_OFF;
212         get_option(&nmi_option, "nmi");
213         if (nmi_option) {
214                 byte &= ~(1 << 7);      /* set NMI */
215                 printk_info("++++++++++set NMI+++++\n");
216         } else {
217                 byte |= (1 << 7);       /* Can not mask NMI from PCI-E and NMI_NOW */
218                 printk_info("++++++++++no set NMI+++++\n");
219         }
220         byte &= ~(1 << 7);
221         if (byte != byte_old) {
222                 outb(byte, 0x70);
223         }
224
225         /* 2.10 IO Trap Settings */
226         abcfg_reg(0x10090, 1 << 16, 1 << 16);
227
228         /* ab index */
229         pci_write_config32(dev, 0xF0, AB_INDX);
230         /* Initialize the real time clock */
231         rtc_init(0);
232
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);
236
237         /* 3.5 Enabling OHCI Prefetch for Performance Enhancement */
238         abcfg_reg(0x80, 1 << 0, 1<< 0);
239
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);
243
244         /* 3.8 Set B-Link Prefetch Mode */
245         abcfg_reg(0x80, 3 << 17, 3 << 17);
246
247         /* 3.9 Enabling Detection of Upstream Interrupts */
248         abcfg_reg(0x94, 1 << 20,1 << 20);
249
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);
253
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);
262
263         /* 3.13:Enabling AB Int_Arbiter Enhancement (for All Revisions) */
264         abcfg_reg(0x10054, 0x0000FFFF, 0x07FF);
265
266         /* 3.14:Enabling L1 on A-link Express */
267         axcfg_reg(0x68, 0x00000003, 0x2);
268         axindxp_reg(0xa0, 0x0000F000, 0x6000);
269
270         abcfg_reg(0x10098, 0xFFFFFFFF, 0x4000);
271         abcfg_reg(0x04, 0xFFFFFFFF, 0x6);
272         printk_info("sm_init() end\n");
273
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);
280 }
281
282 static int lsmbus_recv_byte(device_t dev)
283 {
284         u32 device;
285         struct resource *res;
286         struct bus *pbus;
287
288         device = dev->path.u.i2c.device;
289         pbus = get_pbus_smbus(dev);
290
291         res = find_resource(pbus->dev, 0x10);
292
293         return do_smbus_recv_byte(res->base, device);
294 }
295
296 static int lsmbus_send_byte(device_t dev, u8 val)
297 {
298         u32 device;
299         struct resource *res;
300         struct bus *pbus;
301
302         device = dev->path.u.i2c.device;
303         pbus = get_pbus_smbus(dev);
304
305         res = find_resource(pbus->dev, 0x10);
306
307         return do_smbus_send_byte(res->base, device, val);
308 }
309
310 static int lsmbus_read_byte(device_t dev, u8 address)
311 {
312         u32 device;
313         struct resource *res;
314         struct bus *pbus;
315
316         device = dev->path.u.i2c.device;
317         pbus = get_pbus_smbus(dev);
318
319         res = find_resource(pbus->dev, 0x10);
320
321         return do_smbus_read_byte(res->base, device, address);
322 }
323
324 static int lsmbus_write_byte(device_t dev, u8 address, u8 val)
325 {
326         u32 device;
327         struct resource *res;
328         struct bus *pbus;
329
330         device = dev->path.u.i2c.device;
331         pbus = get_pbus_smbus(dev);
332
333         res = find_resource(pbus->dev, 0x10);
334
335         return do_smbus_write_byte(res->base, device, address, val);
336 }
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,
342 };
343
344 static void sb600_sm_read_resources(device_t dev)
345 {
346         struct resource *res;
347
348         /* Get the normal pci resources of this device */
349         pci_dev_read_resources(dev);
350
351         /* apic */
352         res = new_resource(dev, 0x74);
353         res->base  = 0xfec00000;
354         res->size = 256 * 0x10;
355         res->limit = 0xFFFFFFFFUL;      /* res->base + res->size -1; */
356         res->align = 8;
357         res->gran = 8;
358         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED;
359
360         /* dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER; */
361
362         compact_resources(dev);
363
364 }
365 static void sb600_sm_set_resources(struct device *dev)
366 {
367         struct resource *res;
368
369         pci_dev_set_resources(dev);
370
371         res = find_resource(dev, 0x74);
372         pci_write_config32(dev, 0x74, res->base | 1 << 3);
373 }
374
375 static struct pci_operations lops_pci = {
376         .set_subsystem = pci_dev_set_subsystem,
377 };
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,
382         .init = sm_init,
383         .scan_bus = scan_static_bus,
384         /* .enable           = sb600_enable, */
385         .ops_pci = &lops_pci,
386         .ops_smbus_bus = &lops_smbus_bus,
387 };
388 static struct pci_driver smbus_driver __pci_driver = {
389         .ops = &smbus_ops,
390         .vendor = PCI_VENDOR_ID_ATI,
391         .device = PCI_DEVICE_ID_ATI_SB600_SM,
392 };