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