2 * This file is part of the coreboot project.
4 * Copyright (C) 2005 Advanced Micro Devices, Inc.
5 * Copyright (C) 2010 Advanced Micro Devices, Inc.
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.
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.
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
23 * Description: Amd 8132 support by yhlu
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>
37 #define NPUML 0xD9 /* Non prefetchable upper memory limit */
38 #define NPUMB 0xD8 /* Non prefetchable upper memory base */
40 static void amd8132_walk_children(struct bus *bus,
41 void (*visit)(device_t dev, void *ptr), void *ptr)
44 for(child = bus->children; child; child = child->sibling)
46 if (child->path.type != DEVICE_PATH_PCI) {
49 if (child->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
50 amd8132_walk_children(child->link_list, visit, ptr);
56 struct amd8132_bus_info {
63 static void amd8132_count_dev(device_t dev, void *ptr)
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++;
70 if (PCI_FUNC(dev->path.pci.devfn) > info->max_func) {
71 info->max_func = PCI_FUNC(dev->path.pci.devfn);
76 static void amd8132_pcix_tune_dev(device_t dev, void *ptr)
78 struct amd8132_bus_info *info = ptr;
80 unsigned status, cmd, orig_cmd;
81 unsigned max_read, max_tran;
84 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) {
87 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
91 /* How many siblings does this device have? */
92 sibs = info->master_devices - 1;
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);
98 max_read = (status & PCI_X_STATUS_MAX_READ) >> 21;
99 max_tran = (status & PCI_X_STATUS_MAX_SPLIT) >> 23;
101 if (info->rev == 0x01) { // only a1 need it
102 /* Errata #53 Limit the number of split transactions to avoid starvation */
104 /* At most 2 outstanding split transactions when we have
105 * 3 or more bus master devices on the bus.
111 else if (sibs == 1) {
112 /* At most 4 outstanding split transactions when we have
113 * 2 bus master devices on the bus.
120 /* At most 8 outstanding split transactions when we have
121 * only one bus master device on the bus.
129 if (max_read != ((cmd & PCI_X_CMD_MAX_READ) >> 2)) {
130 cmd &= ~PCI_X_CMD_MAX_READ;
131 cmd |= max_read << 2;
133 if (max_tran != ((cmd & PCI_X_CMD_MAX_SPLIT) >> 4)) {
134 cmd &= ~PCI_X_CMD_MAX_SPLIT;
135 cmd |= max_tran << 4;
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);
146 static unsigned int amd8132_scan_bus(struct bus *bus,
147 unsigned min_devfn, unsigned max_devfn, unsigned int max)
149 struct amd8132_bus_info info;
153 /* Find the children on the bus */
154 max = pci_scan_bus(bus, min_devfn, max_devfn, max);
156 /* Find the revision of the 8132 */
157 info.rev = pci_read_config8(bus->dev, PCI_CLASS_REVISION);
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);
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);
167 /* Examine the bus and find out how loaded it is */
169 info.master_devices = 0;
170 amd8132_walk_children(bus, amd8132_count_dev, &info);
173 /* Disable the bus if there are no devices on it
178 /* Disable all of my children */
179 disable_children(bus);
181 /* Remember the device is disabled */
182 bus->dev->enabled = 0;
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);
193 /* If we are in conventional PCI mode nothing more is necessary.
195 if (PCI_X_SSTATUS_MFREQ(info.sstatus) == PCI_X_SSTATUS_CONVENTIONAL_PCI) {
199 /* Tune the devices on the bus */
200 amd8132_walk_children(bus, amd8132_pcix_tune_dev, &info);
205 static unsigned int amd8132_scan_bridge(device_t dev, unsigned int max)
207 return do_pci_scan_bridge(dev, max, amd8132_scan_bus);
211 static void amd8132_pcix_init(device_t dev)
217 /* Find the revision of the 8132 */
218 chip_rev = pci_read_config8(dev, PCI_CLASS_REVISION);
220 /* Enable memory write and invalidate ??? */
221 dword = pci_read_config32(dev, 0x04);
223 dword &= ~(1<<6); // PERSP Parity Error Response
224 pci_write_config32(dev, 0x04, dword);
226 if (chip_rev == 0x01) {
228 byte = pci_read_config8(dev, 0x0c);
230 pci_write_config8(dev, 0x0c, 0x10);
234 dword = pci_read_config32(dev, 0x40);
236 pci_write_config32(dev, 0x40, dword);
241 /* Set up error reporting, enable all */
242 /* system error enable */
243 dword = pci_read_config32(dev, 0x04);
245 pci_write_config32(dev, 0x04, dword);
247 /* system and error parity enable */
248 dword = pci_read_config32(dev, 0x3c);
250 pci_write_config32(dev, 0x3c, dword);
252 dword = pci_read_config32(dev, 0x40);
253 // dword &= ~(1<<31); /* WriteChainEnable */
255 dword |= (1<<7);// must set to 1
256 dword |= (3<<21); //PCIErrorSerrDisable
257 pci_write_config32(dev, 0x40, dword);
259 /* EXTARB = 1, COMPAT = 0 */
260 dword = pci_read_config32(dev, 0x48);
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);
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);
276 /* Disable Single-Bit-Error Correction [30] = 0 */
277 dword = pci_read_config32(dev, 0x70);
279 pci_write_config32(dev, 0x70, dword);
282 dword = pci_read_config32(dev, 0xd4);
284 pci_write_config32(dev, 0xd4, dword);
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);
293 /* Set up CRC flood enable */
294 dword = pci_read_config32(dev, 0xc0);
295 if(dword) { /* do device A only */
297 dword = pci_read_config32(dev, 0xc4);
299 pci_write_config32(dev, 0xc4, dword);
300 dword = pci_read_config32(dev, 0xc8);
302 pci_write_config32(dev, 0xc8, dword);
305 if (chip_rev == 0x11) {
306 /* [18] Clock Gate Enable = 1 */
307 dword = pci_read_config32(dev, 0xf0);
309 pci_write_config32(dev, 0xf0, dword);
316 #define BRIDGE_40_BIT_SUPPORT 0
317 #if BRIDGE_40_BIT_SUPPORT
318 static void bridge_read_resources(struct device *dev)
320 struct resource *res;
321 pci_bus_read_resources(dev);
322 res = find_resource(dev, PCI_MEMORY_BASE);
324 res->limit = 0xffffffffffULL;
328 static void bridge_set_resources(struct device *dev)
330 struct resource *res;
331 res = find_resource(dev, PCI_MEMORY_BASE);
333 resource_t base, end;
334 /* set the memory range */
335 dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
336 res->flags |= IORESOURCE_STORED;
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);
344 report_resource_stored(dev, res, "");
346 pci_dev_set_resources(dev);
348 #endif /* BRIDGE_40_BIT_SUPPORT */
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,
355 .read_resources = pci_bus_read_resources,
356 .set_resources = pci_dev_set_resources,
358 .enable_resources = pci_bus_enable_resources,
359 .init = amd8132_pcix_init,
360 .scan_bus = amd8132_scan_bridge,
361 .reset_bus = pci_bus_reset,
364 static const struct pci_driver pcix_driver __pci_driver = {
366 .vendor = PCI_VENDOR_ID_AMD,
370 static void ioapic_enable(device_t dev)
374 value = pci_read_config32(dev, 0x44);
376 value |= ((1 << 1) | (1 << 0));
378 value &= ~((1 << 1) | (1 << 0));
380 pci_write_config32(dev, 0x44, value);
382 static void amd8132_ioapic_init(device_t dev)
387 /* Find the revision of the 8132 */
388 chip_rev = pci_read_config8(dev, PCI_CLASS_REVISION);
390 if (chip_rev == 0x01) {
393 dword = pci_read_config32(dev, 0xc8);
395 pci_write_config32(dev, 0xc8, dword);
401 if( (chip_rev == 0x11) ||(chip_rev == 0x12) ) {
404 dword = pci_read_config32(dev, 0x80);
406 pci_write_config32(dev, 0x80, dword);
407 dword = pci_read_config32(dev, 0x88);
409 pci_write_config32(dev, 0x88, dword);
412 dword = pci_read_config32(dev, 0x7c);
415 pci_write_config32(dev, 0x7c, dword);
420 static struct pci_operations pci_ops_pci_dev = {
421 .set_subsystem = pci_dev_set_subsystem,
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,
429 .enable = ioapic_enable,
430 .ops_pci = &pci_ops_pci_dev,
433 static const struct pci_driver ioapic_driver __pci_driver = {
435 .vendor = PCI_VENDOR_ID_AMD,