2 * This file is part of the coreboot project.
4 * Copyright (C) 2005,2010 Advanced Micro Devices, Inc.
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.
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.
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
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>
32 #define NPUML 0xD9 /* Non prefetchable upper memory limit */
33 #define NPUMB 0xD8 /* Non prefetchable upper memory base */
35 static void amd8132_walk_children(struct bus *bus,
36 void (*visit)(device_t dev, void *ptr), void *ptr)
39 for(child = bus->children; child; child = child->sibling)
41 if (child->path.type != DEVICE_PATH_PCI) {
44 if (child->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
45 amd8132_walk_children(child->link_list, visit, ptr);
51 struct amd8132_bus_info {
58 static void amd8132_count_dev(device_t dev, void *ptr)
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++;
65 if (PCI_FUNC(dev->path.pci.devfn) > info->max_func) {
66 info->max_func = PCI_FUNC(dev->path.pci.devfn);
71 static void amd8132_pcix_tune_dev(device_t dev, void *ptr)
73 struct amd8132_bus_info *info = ptr;
75 unsigned status, cmd, orig_cmd;
76 unsigned max_read, max_tran;
79 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) {
82 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
86 /* How many siblings does this device have? */
87 sibs = info->master_devices - 1;
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);
93 max_read = (status & PCI_X_STATUS_MAX_READ) >> 21;
94 max_tran = (status & PCI_X_STATUS_MAX_SPLIT) >> 23;
96 if (info->rev == 0x01) { // only a1 need it
97 /* Errata #53 Limit the number of split transactions to avoid starvation */
99 /* At most 2 outstanding split transactions when we have
100 * 3 or more bus master devices on the bus.
106 else if (sibs == 1) {
107 /* At most 4 outstanding split transactions when we have
108 * 2 bus master devices on the bus.
115 /* At most 8 outstanding split transactions when we have
116 * only one bus master device on the bus.
124 if (max_read != ((cmd & PCI_X_CMD_MAX_READ) >> 2)) {
125 cmd &= ~PCI_X_CMD_MAX_READ;
126 cmd |= max_read << 2;
128 if (max_tran != ((cmd & PCI_X_CMD_MAX_SPLIT) >> 4)) {
129 cmd &= ~PCI_X_CMD_MAX_SPLIT;
130 cmd |= max_tran << 4;
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);
141 static unsigned int amd8132_scan_bus(struct bus *bus,
142 unsigned min_devfn, unsigned max_devfn, unsigned int max)
144 struct amd8132_bus_info info;
148 /* Find the children on the bus */
149 max = pci_scan_bus(bus, min_devfn, max_devfn, max);
151 /* Find the revision of the 8132 */
152 info.rev = pci_read_config8(bus->dev, PCI_CLASS_REVISION);
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);
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);
162 /* Examine the bus and find out how loaded it is */
164 info.master_devices = 0;
165 amd8132_walk_children(bus, amd8132_count_dev, &info);
168 /* Disable the bus if there are no devices on it
173 /* Disable all of my children */
174 disable_children(bus);
176 /* Remember the device is disabled */
177 bus->dev->enabled = 0;
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);
188 /* If we are in conventional PCI mode nothing more is necessary.
190 if (PCI_X_SSTATUS_MFREQ(info.sstatus) == PCI_X_SSTATUS_CONVENTIONAL_PCI) {
194 /* Tune the devices on the bus */
195 amd8132_walk_children(bus, amd8132_pcix_tune_dev, &info);
200 static unsigned int amd8132_scan_bridge(device_t dev, unsigned int max)
202 return do_pci_scan_bridge(dev, max, amd8132_scan_bus);
206 static void amd8132_pcix_init(device_t dev)
212 /* Find the revision of the 8132 */
213 chip_rev = pci_read_config8(dev, PCI_CLASS_REVISION);
215 /* Enable memory write and invalidate ??? */
216 dword = pci_read_config32(dev, 0x04);
218 dword &= ~(1<<6); // PERSP Parity Error Response
219 pci_write_config32(dev, 0x04, dword);
221 if (chip_rev == 0x01) {
223 byte = pci_read_config8(dev, 0x0c);
225 pci_write_config8(dev, 0x0c, 0x10);
229 dword = pci_read_config32(dev, 0x40);
231 pci_write_config32(dev, 0x40, dword);
236 /* Set up error reporting, enable all */
237 /* system error enable */
238 dword = pci_read_config32(dev, 0x04);
240 pci_write_config32(dev, 0x04, dword);
242 /* system and error parity enable */
243 dword = pci_read_config32(dev, 0x3c);
245 pci_write_config32(dev, 0x3c, dword);
247 dword = pci_read_config32(dev, 0x40);
248 // dword &= ~(1<<31); /* WriteChainEnable */
250 dword |= (1<<7);// must set to 1
251 dword |= (3<<21); //PCIErrorSerrDisable
252 pci_write_config32(dev, 0x40, dword);
254 /* EXTARB = 1, COMPAT = 0 */
255 dword = pci_read_config32(dev, 0x48);
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);
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);
271 /* Disable Single-Bit-Error Correction [30] = 0 */
272 dword = pci_read_config32(dev, 0x70);
274 pci_write_config32(dev, 0x70, dword);
277 dword = pci_read_config32(dev, 0xd4);
279 pci_write_config32(dev, 0xd4, dword);
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);
288 /* Set up CRC flood enable */
289 dword = pci_read_config32(dev, 0xc0);
290 if(dword) { /* do device A only */
292 dword = pci_read_config32(dev, 0xc4);
294 pci_write_config32(dev, 0xc4, dword);
295 dword = pci_read_config32(dev, 0xc8);
297 pci_write_config32(dev, 0xc8, dword);
300 if (chip_rev == 0x11) {
301 /* [18] Clock Gate Enable = 1 */
302 dword = pci_read_config32(dev, 0xf0);
304 pci_write_config32(dev, 0xf0, dword);
311 #define BRIDGE_40_BIT_SUPPORT 0
312 #if BRIDGE_40_BIT_SUPPORT
313 static void bridge_read_resources(struct device *dev)
315 struct resource *res;
316 pci_bus_read_resources(dev);
317 res = find_resource(dev, PCI_MEMORY_BASE);
319 res->limit = 0xffffffffffULL;
323 static void bridge_set_resources(struct device *dev)
325 struct resource *res;
326 res = find_resource(dev, PCI_MEMORY_BASE);
328 resource_t base, end;
329 /* set the memory range */
330 dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
331 res->flags |= IORESOURCE_STORED;
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);
339 report_resource_stored(dev, res, "");
341 pci_dev_set_resources(dev);
343 #endif /* BRIDGE_40_BIT_SUPPORT */
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,
350 .read_resources = pci_bus_read_resources,
351 .set_resources = pci_dev_set_resources,
353 .enable_resources = pci_bus_enable_resources,
354 .init = amd8132_pcix_init,
355 .scan_bus = amd8132_scan_bridge,
356 .reset_bus = pci_bus_reset,
359 static const struct pci_driver pcix_driver __pci_driver = {
361 .vendor = PCI_VENDOR_ID_AMD,
365 static void ioapic_enable(device_t dev)
369 value = pci_read_config32(dev, 0x44);
371 value |= ((1 << 1) | (1 << 0));
373 value &= ~((1 << 1) | (1 << 0));
375 pci_write_config32(dev, 0x44, value);
377 static void amd8132_ioapic_init(device_t dev)
382 /* Find the revision of the 8132 */
383 chip_rev = pci_read_config8(dev, PCI_CLASS_REVISION);
385 if (chip_rev == 0x01) {
388 dword = pci_read_config32(dev, 0xc8);
390 pci_write_config32(dev, 0xc8, dword);
396 if( (chip_rev == 0x11) ||(chip_rev == 0x12) ) {
399 dword = pci_read_config32(dev, 0x80);
401 pci_write_config32(dev, 0x80, dword);
402 dword = pci_read_config32(dev, 0x88);
404 pci_write_config32(dev, 0x88, dword);
407 dword = pci_read_config32(dev, 0x7c);
410 pci_write_config32(dev, 0x7c, dword);
415 static struct pci_operations pci_ops_pci_dev = {
416 .set_subsystem = pci_dev_set_subsystem,
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,
424 .enable = ioapic_enable,
425 .ops_pci = &pci_ops_pci_dev,
428 static const struct pci_driver ioapic_driver __pci_driver = {
430 .vendor = PCI_VENDOR_ID_AMD,