42babe79dda6c2215528dfeb0b0d9703864b6797
[coreboot.git] / src / devices / pcix_device.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2005 Linux Networx
5  * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
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; version 2 of the License.
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/pcix.h>
26
27 static void pcix_tune_dev(device_t dev)
28 {
29         unsigned cap;
30         unsigned status, orig_cmd, cmd;
31         unsigned max_read, max_tran;
32
33         if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) {
34                 return;
35         }
36         cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
37         if (!cap) {
38                 return;
39         }
40         printk(BIOS_DEBUG, "%s PCI-X tuning\n", dev_path(dev));
41         status = pci_read_config32(dev, cap + PCI_X_STATUS);
42         orig_cmd = cmd = pci_read_config16(dev,cap + PCI_X_CMD);
43
44         max_read = (status & PCI_X_STATUS_MAX_READ) >> 21;
45         max_tran = (status & PCI_X_STATUS_MAX_SPLIT) >> 23;
46         if (max_read != ((cmd & PCI_X_CMD_MAX_READ) >> 2)) {
47                 cmd &= ~PCI_X_CMD_MAX_READ;
48                 cmd |= max_read << 2;
49         }
50         if (max_tran != ((cmd & PCI_X_CMD_MAX_SPLIT) >> 4)) {
51                 cmd &= ~PCI_X_CMD_MAX_SPLIT;
52                 cmd |= max_tran << 4;
53         }
54         /* Don't attempt to handle PCI-X errors */
55         cmd &= ~PCI_X_CMD_DPERR_E;
56         /* Enable Relaxed Ordering */
57         cmd |= PCI_X_CMD_ERO;
58         if (orig_cmd != cmd) {
59                 pci_write_config16(dev, cap + PCI_X_CMD, cmd);
60         }
61 }
62
63 static void pcix_tune_bus(struct bus *bus)
64 {
65         device_t child;
66         for(child = bus->children; child; child = child->sibling)
67                 pcix_tune_dev(child);
68 }
69
70 const char *pcix_speed(unsigned sstatus)
71 {
72         static const char conventional[] = "Conventional PCI";
73         static const char pcix_66mhz[] = "66MHz PCI-X";
74         static const char pcix_100mhz[] = "100MHz PCI-X";
75         static const char pcix_133mhz[] = "133MHz PCI-X";
76         static const char pcix_266mhz[] = "266MHz PCI-X";
77         static const char pcix_533mhz[] = "533MHZ PCI-X";
78         static const char unknown[] = "Unknown";
79
80         const char *result;
81         result = unknown;
82         switch(PCI_X_SSTATUS_MFREQ(sstatus)) {
83         case PCI_X_SSTATUS_CONVENTIONAL_PCI:
84                 result = conventional;
85                 break;
86         case PCI_X_SSTATUS_MODE1_66MHZ:
87                 result = pcix_66mhz;
88                 break;
89         case PCI_X_SSTATUS_MODE1_100MHZ:
90                 result = pcix_100mhz;
91                 break;
92
93         case PCI_X_SSTATUS_MODE1_133MHZ:
94                 result = pcix_133mhz;
95                 break;
96
97         case PCI_X_SSTATUS_MODE2_266MHZ_REF_66MHZ:
98         case PCI_X_SSTATUS_MODE2_266MHZ_REF_100MHZ:
99         case PCI_X_SSTATUS_MODE2_266MHZ_REF_133MHZ:
100                 result = pcix_266mhz;
101                 break;
102
103         case PCI_X_SSTATUS_MODE2_533MHZ_REF_66MHZ:
104         case PCI_X_SSTATUS_MODE2_533MHZ_REF_100MHZ:
105         case PCI_X_SSTATUS_MODE2_533MHZ_REF_133MHZ:
106                 result = pcix_533mhz;
107                 break;
108         }
109         return result;
110 }
111
112 unsigned int pcix_scan_bridge(device_t dev, unsigned int max)
113 {
114         unsigned pos;
115         unsigned sstatus;
116
117         max = do_pci_scan_bridge(dev, max, pci_scan_bus);
118         /* Find the PCI-X capability */
119         pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
120         sstatus = pci_read_config16(dev, pos + PCI_X_SEC_STATUS);
121
122         if (PCI_X_SSTATUS_MFREQ(sstatus) != PCI_X_SSTATUS_CONVENTIONAL_PCI) {
123                 pcix_tune_bus(dev->link_list);
124         }
125
126         /* Print the PCI-X bus speed */
127         printk(BIOS_DEBUG, "PCI: %02x: %s\n", dev->link_list->secondary, pcix_speed(sstatus));
128
129         return max;
130 }
131
132 /** Default device operations for PCI-X bridges */
133 static struct pci_operations pcix_bus_ops_pci = {
134         .set_subsystem = 0,
135 };
136
137 struct device_operations default_pcix_ops_bus = {
138         .read_resources   = pci_bus_read_resources,
139         .set_resources    = pci_dev_set_resources,
140         .enable_resources = pci_bus_enable_resources,
141         .init             = 0,
142         .scan_bus         = pcix_scan_bridge,
143         .enable           = 0,
144         .reset_bus        = pci_bus_reset,
145         .ops_pci          = &pcix_bus_ops_pci,
146 };