2ce46cd16a2805f4523ebb35d1a81185fb6d0944
[coreboot.git] / src / southbridge / amd / amd8132 / amd8132_bridge.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2005 Advanced Micro Devices, Inc.
5  * Copyright (C) 2010 Advanced Micro Devices, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 /*
23  * Description: Amd 8132 support by yhlu
24  */
25
26 #include <console/console.h>
27 #include <device/device.h>
28 #include <device/pci.h>
29 #include <device/pci_ids.h>
30 #include <device/pci_ops.h>
31 #include <pc80/mc146818rtc.h>
32 #include <device/pci_def.h>
33 #include <device/pcix.h>
34
35 #define NMI_OFF 0
36
37 #define NPUML 0xD9      /* Non prefetchable upper memory limit */
38 #define NPUMB 0xD8      /* Non prefetchable upper memory base */
39
40 static void amd8132_walk_children(struct bus *bus,
41         void (*visit)(device_t dev, void *ptr), void *ptr)
42 {
43         device_t child;
44         for(child = bus->children; child; child = child->sibling)
45         {
46                 if (child->path.type != DEVICE_PATH_PCI) {
47                         continue;
48                 }
49                 if (child->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
50                         amd8132_walk_children(child->link_list, visit, ptr);
51                 }
52                 visit(child, ptr);
53         }
54 }
55
56 struct amd8132_bus_info {
57         unsigned sstatus;
58         unsigned rev;
59         int master_devices;
60         int max_func;
61 };
62
63 static void amd8132_count_dev(device_t dev, void *ptr)
64 {
65         struct amd8132_bus_info *info = ptr;
66         /* Don't count pci bridges */
67         if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
68                 info->master_devices++;
69         }
70         if (PCI_FUNC(dev->path.pci.devfn) > info->max_func) {
71                 info->max_func = PCI_FUNC(dev->path.pci.devfn);
72         }
73 }
74
75
76 static void amd8132_pcix_tune_dev(device_t dev, void *ptr)
77 {
78         struct amd8132_bus_info *info = ptr;
79         unsigned cap;
80         unsigned status, cmd, orig_cmd;
81         unsigned max_read, max_tran;
82         int  sibs;
83
84         if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) {
85                 return;
86         }
87         cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
88         if (!cap) {
89                 return;
90         }
91         /* How many siblings does this device have? */
92         sibs = info->master_devices - 1;
93
94         printk(BIOS_DEBUG, "%s AMD8132 PCI-X tuning\n", dev_path(dev));
95         status = pci_read_config32(dev, cap + PCI_X_STATUS);
96         orig_cmd = cmd = pci_read_config16(dev,cap + PCI_X_CMD);
97
98         max_read = (status & PCI_X_STATUS_MAX_READ) >> 21;
99         max_tran = (status & PCI_X_STATUS_MAX_SPLIT) >> 23;
100
101         if (info->rev == 0x01) { // only a1 need it
102                 /* Errata #53 Limit the number of split transactions to avoid starvation */
103                 if (sibs >= 2) {
104                         /* At most 2 outstanding split transactions when we have
105                         * 3 or more bus master devices on the bus.
106                         */
107                         if (max_tran > 1) {
108                                 max_tran = 1;
109                         }
110                 }
111                 else if (sibs == 1) {
112                         /* At most 4 outstanding split transactions when we have
113                         * 2 bus master devices on the bus.
114                         */
115                         if (max_tran > 3) {
116                                 max_tran = 3;
117                         }
118                 }
119                 else {
120                         /* At most 8 outstanding split transactions when we have
121                         * only one bus master device on the bus.
122                         */
123                         if (max_tran > 4) {
124                                 max_tran = 4;
125                         }
126                 }
127         }
128
129         if (max_read != ((cmd & PCI_X_CMD_MAX_READ) >> 2)) {
130                 cmd &= ~PCI_X_CMD_MAX_READ;
131                 cmd |= max_read << 2;
132                 }
133         if (max_tran != ((cmd & PCI_X_CMD_MAX_SPLIT) >> 4)) {
134                 cmd &= ~PCI_X_CMD_MAX_SPLIT;
135                 cmd |= max_tran << 4;
136         }
137
138         /* Don't attempt to handle PCI-X errors */
139         cmd &= ~PCI_X_CMD_DPERR_E;
140         if (orig_cmd != cmd) {
141                 pci_write_config16(dev, cap + PCI_X_CMD, cmd);
142         }
143
144
145 }
146 static unsigned int amd8132_scan_bus(struct bus *bus,
147         unsigned min_devfn, unsigned max_devfn, unsigned int max)
148 {
149         struct amd8132_bus_info info;
150         unsigned pos;
151
152
153         /* Find the children on the bus */
154         max = pci_scan_bus(bus, min_devfn, max_devfn, max);
155
156         /* Find the revision of the 8132 */
157         info.rev = pci_read_config8(bus->dev, PCI_CLASS_REVISION);
158
159         /* Find the pcix capability and get the secondary bus status */
160         pos = pci_find_capability(bus->dev, PCI_CAP_ID_PCIX);
161         info.sstatus = pci_read_config16(bus->dev, pos + PCI_X_SEC_STATUS);
162
163         /* Print the PCI-X bus speed */
164         printk(BIOS_DEBUG, "PCI: %02x: %s sstatus=%04x rev=%02x \n", bus->secondary, pcix_speed(info.sstatus), info.sstatus, info.rev);
165
166
167         /* Examine the bus and find out how loaded it is */
168         info.max_func = 0;
169         info.master_devices  = 0;
170         amd8132_walk_children(bus, amd8132_count_dev, &info);
171
172 #if 0
173         /* Disable the bus if there are no devices on it
174          */
175         if (!bus->children)
176         {
177                 unsigned pcix_misc;
178                 /* Disable all of my children */
179                 disable_children(bus);
180
181                 /* Remember the device is disabled */
182                 bus->dev->enabled = 0;
183
184                 /* Disable the PCI-X clocks */
185                 pcix_misc = pci_read_config32(bus->dev, 0x40);
186                 pcix_misc &= ~(0x1f << 16);
187                 pci_write_config32(bus->dev, 0x40, pcix_misc);
188
189                 return max;
190         }
191 #endif
192
193         /* If we are in conventional PCI mode nothing more is necessary.
194          */
195         if (PCI_X_SSTATUS_MFREQ(info.sstatus) == PCI_X_SSTATUS_CONVENTIONAL_PCI) {
196                 return max;
197         }
198
199         /* Tune the devices on the bus */
200         amd8132_walk_children(bus, amd8132_pcix_tune_dev, &info);
201
202         return max;
203 }
204
205 static unsigned int amd8132_scan_bridge(device_t dev, unsigned int max)
206 {
207         return do_pci_scan_bridge(dev, max, amd8132_scan_bus);
208 }
209
210
211 static void amd8132_pcix_init(device_t dev)
212 {
213         uint32_t dword;
214         uint8_t byte;
215         unsigned chip_rev;
216
217         /* Find the revision of the 8132 */
218         chip_rev = pci_read_config8(dev, PCI_CLASS_REVISION);
219
220         /* Enable memory write and invalidate ??? */
221         dword = pci_read_config32(dev, 0x04);
222         dword |= 0x10;
223         dword &= ~(1<<6); // PERSP Parity Error Response
224         pci_write_config32(dev, 0x04, dword);
225
226         if (chip_rev == 0x01) {
227                 /* Errata #37 */
228                 byte = pci_read_config8(dev, 0x0c);
229                 if(byte == 0x08 )
230                         pci_write_config8(dev, 0x0c, 0x10);
231
232 #if 0
233                 /* Errata #59*/
234                 dword = pci_read_config32(dev, 0x40);
235                 dword &= ~(1<<31);
236                 pci_write_config32(dev, 0x40, dword);
237 #endif
238
239         }
240
241         /* Set up error reporting, enable all */
242         /* system error enable */
243         dword = pci_read_config32(dev, 0x04);
244         dword |= (1<<8);
245         pci_write_config32(dev, 0x04, dword);
246
247         /* system and error parity enable */
248         dword = pci_read_config32(dev, 0x3c);
249         dword |= (3<<16);
250         pci_write_config32(dev, 0x3c, dword);
251
252         dword = pci_read_config32(dev, 0x40);
253 //        dword &= ~(1<<31); /* WriteChainEnable */
254         dword |= (1<<31);
255         dword |= (1<<7);// must set to 1
256         dword |= (3<<21); //PCIErrorSerrDisable
257         pci_write_config32(dev, 0x40, dword);
258
259         /* EXTARB = 1, COMPAT = 0 */
260         dword = pci_read_config32(dev, 0x48);
261         dword |= (1<<3);
262         dword &= ~(1<<0);
263         dword |= (1<<15); //CLEARPCILOG_L
264         dword |= (1<<19); //PERR FATAL Enable
265         dword |= (1<<22); // SERR FATAL Enable
266         dword |= (1<<23); // LPMARBENABLE
267         dword |= (0x61<<24); //LPMARBCOUNT
268         pci_write_config32(dev, 0x48, dword);
269
270         dword = pci_read_config32(dev, 0x4c);
271         dword |= (1<<6); //intial prefetch for memory read line request
272         dword |= (1<<9); //continuous prefetch Enable for memory read line request
273         pci_write_config32(dev, 0x4c, dword);
274
275
276        /* Disable Single-Bit-Error Correction [30] = 0 */
277         dword = pci_read_config32(dev, 0x70);
278         dword &= ~(1<<30);
279         pci_write_config32(dev, 0x70, dword);
280
281         //link
282         dword = pci_read_config32(dev, 0xd4);
283         dword |= (0x5c<<16);
284         pci_write_config32(dev, 0xd4, dword);
285
286         /* TxSlack0 [16:17] = 0, RxHwLookahdEn0 [18] = 1, TxSlack1 [24:25] = 0, RxHwLookahdEn1 [26] = 1 */
287         dword = pci_read_config32(dev, 0xdc);
288         dword |= (1<<1) |  (1<<4); // stream disable 1 to 0 , DBLINSRATE
289         dword |= (1<<18)|(1<<26);
290         dword &= ~((3<<16)|(3<<24));
291         pci_write_config32(dev, 0xdc, dword);
292
293         /* Set up CRC flood enable */
294         dword = pci_read_config32(dev, 0xc0);
295         if(dword) {  /* do device A only */
296 #if 0
297                 dword = pci_read_config32(dev, 0xc4);
298                 dword |= (1<<1);
299                 pci_write_config32(dev, 0xc4, dword);
300                 dword = pci_read_config32(dev, 0xc8);
301                 dword |= (1<<1);
302                 pci_write_config32(dev, 0xc8, dword);
303 #endif
304
305                 if (chip_rev == 0x11) {
306                         /* [18] Clock Gate Enable = 1 */
307                         dword = pci_read_config32(dev, 0xf0);
308                         dword |= 0x00040008;
309                         pci_write_config32(dev, 0xf0, dword);
310                 }
311
312         }
313         return;
314 }
315
316 #define BRIDGE_40_BIT_SUPPORT 0
317 #if BRIDGE_40_BIT_SUPPORT
318 static void bridge_read_resources(struct device *dev)
319 {
320         struct resource *res;
321         pci_bus_read_resources(dev);
322         res = find_resource(dev, PCI_MEMORY_BASE);
323         if (res) {
324                 res->limit = 0xffffffffffULL;
325         }
326 }
327
328 static void bridge_set_resources(struct device *dev)
329 {
330         struct resource *res;
331         res = find_resource(dev, PCI_MEMORY_BASE);
332         if (res) {
333                 resource_t base, end;
334                 /* set the memory range */
335                 dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
336                 res->flags |= IORESOURCE_STORED;
337                 base = res->base;
338                 end  = resource_end(res);
339                 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
340                 pci_write_config8(dev, NPUML, (base >> 32) & 0xff);
341                 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
342                 pci_write_config8(dev, NPUMB, (end >> 32) & 0xff);
343
344                 report_resource_stored(dev, res, "");
345         }
346         pci_dev_set_resources(dev);
347 }
348 #endif /* BRIDGE_40_BIT_SUPPORT */
349
350 static struct device_operations pcix_ops  = {
351 #if BRIDGE_40_BIT_SUPPORT
352         .read_resources   = bridge_read_resources,
353         .set_resources    = bridge_set_resources,
354 #else
355         .read_resources   = pci_bus_read_resources,
356         .set_resources    = pci_dev_set_resources,
357 #endif
358         .enable_resources = pci_bus_enable_resources,
359         .init             = amd8132_pcix_init,
360         .scan_bus         = amd8132_scan_bridge,
361         .reset_bus        = pci_bus_reset,
362 };
363
364 static const struct pci_driver pcix_driver __pci_driver = {
365         .ops    = &pcix_ops,
366         .vendor = PCI_VENDOR_ID_AMD,
367         .device = 0x7458,
368 };
369
370 static void ioapic_enable(device_t dev)
371 {
372         uint32_t value;
373
374         value = pci_read_config32(dev, 0x44);
375         if (dev->enabled) {
376                 value |= ((1 << 1) | (1 << 0));
377         } else {
378                 value &= ~((1 << 1) | (1 << 0));
379         }
380         pci_write_config32(dev, 0x44, value);
381 }
382 static void amd8132_ioapic_init(device_t dev)
383 {
384         uint32_t dword;
385         unsigned chip_rev;
386
387         /* Find the revision of the 8132 */
388         chip_rev = pci_read_config8(dev, PCI_CLASS_REVISION);
389
390         if (chip_rev == 0x01) {
391 #if 0
392                 /* Errata #43 */
393                 dword = pci_read_config32(dev, 0xc8);
394                 dword |= (0x3<<23);
395                 pci_write_config32(dev, 0xc8, dword);
396 #endif
397
398         }
399
400
401         if( (chip_rev == 0x11) ||(chip_rev == 0x12) ) {
402                 //for b1 b2
403                 /* Errata #73 */
404                 dword = pci_read_config32(dev, 0x80);
405                 dword |= (0x1f<<5);
406                 pci_write_config32(dev, 0x80, dword);
407                 dword = pci_read_config32(dev, 0x88);
408                 dword |= (0x1f<<5);
409                 pci_write_config32(dev, 0x88, dword);
410
411                 /* Errata #74 */
412                 dword = pci_read_config32(dev, 0x7c);
413                 dword &= ~(0x3<<30);
414                 dword |= (0x01<<30);
415                 pci_write_config32(dev, 0x7c, dword);
416         }
417
418 }
419
420 static struct pci_operations pci_ops_pci_dev = {
421         .set_subsystem    = pci_dev_set_subsystem,
422 };
423 static struct device_operations ioapic_ops = {
424         .read_resources   = pci_dev_read_resources,
425         .set_resources    = pci_dev_set_resources,
426         .enable_resources = pci_dev_enable_resources,
427         .init             = amd8132_ioapic_init,
428         .scan_bus         = 0,
429         .enable           = ioapic_enable,
430         .ops_pci          = &pci_ops_pci_dev,
431 };
432
433 static const struct pci_driver ioapic_driver __pci_driver = {
434         .ops    = &ioapic_ops,
435         .vendor = PCI_VENDOR_ID_AMD,
436         .device = 0x7459,
437
438 };