blob: 3e5a8353a28df29ae777dec575beb0976645dda9 [file] [log] [blame]
Uwe Hermannb80dbf02007-04-22 19:08:13 +00001/*
2 * This file is part of the LinuxBIOS project.
3 *
4 * Copyright (C) 2005 Linux Networx
5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6 * Copyright (C) 2005 Ronald G. Minnich <rminnich@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
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 */
Yinghai Lu304f24c2005-07-08 02:56:47 +000021
22#include <console/console.h>
23#include <device/device.h>
24#include <device/pci.h>
25#include <device/pci_ids.h>
26#include <device/cardbus.h>
27
28/* I don't think this code is quite correct but it is close.
29 * Anyone with a cardbus bridge and a little time should be able
30 * to make it usable quickly. -- Eric Biederman 24 March 2005
31 */
32
33/*
34 * IO should be max 256 bytes. However, since we may
35 * have a P2P bridge below a cardbus bridge, we need 4K.
36 */
37#define CARDBUS_IO_SIZE (4096)
38#define CARDBUS_MEM_SIZE (32*1024*1024)
39
40static void cardbus_record_bridge_resource(
41 device_t dev, resource_t moving, resource_t min_size,
42 unsigned index, unsigned long type)
43{
44 /* Initiliaze the constraints on the current bus */
45 struct resource *resource;
46 resource = 0;
47 if (moving) {
48 unsigned long gran;
49 resource_t step;
50 resource = new_resource(dev, index);
51 resource->size = 0;
52 gran = 0;
53 step = 1;
54 while((moving & step) == 0) {
55 gran += 1;
56 step <<= 1;
57 }
58 resource->gran = gran;
59 resource->align = gran;
60 resource->limit = moving | (step - 1);
61 resource->flags = type;
62 /* Don't let the minimum size exceed what we
63 * can put in the resource.
64 */
65 if ((min_size - 1) > resource->limit) {
66 min_size = resource->limit + 1;
67 }
68 resource->size = min_size;
69 }
70 return;
71}
72
73static void cardbus_size_bridge_resource(device_t dev, unsigned index)
74{
75 struct resource *resource;
76 resource_t min_size;
77 resource = find_resource(dev, index);
78 if (resource) {
79 min_size = resource->size;
80 compute_allocate_resource(&dev->link[0], resource,
81 resource->flags, resource->flags);
82 /* Allways allocate at least the miniumum size to a
83 * cardbus bridge in case a new card is plugged in.
84 */
85 if (resource->size < min_size) {
86 resource->size = min_size;
87 }
88 }
89}
90
91void cardbus_read_resources(device_t dev)
92{
93 resource_t moving_base, moving_limit, moving;
94 unsigned long type;
95 uint16_t ctl;
Ronald G. Minnich43225bc2005-11-22 00:07:02 +000096 unsigned long index;
97
98 /* See if needs a card control registers base address */
99
100 pci_get_resource(dev, PCI_BASE_ADDRESS_0);
101
102 compact_resources(dev);
103
Yinghai Lu304f24c2005-07-08 02:56:47 +0000104 /* See which bridge I/O resources are implemented */
105 moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_0);
106 moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_0);
107 moving = moving_base & moving_limit;
108
109 /* Initialize the io space constraints on the current bus */
110 cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE,
111 PCI_CB_IO_BASE_0, IORESOURCE_IO);
112 cardbus_size_bridge_resource(dev, PCI_CB_IO_BASE_0);
113
114 /* See which bridge I/O resources are implemented */
115 moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_1);
116 moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_1);
117 moving = moving_base & moving_limit;
118
119 /* Initialize the io space constraints on the current bus */
120 cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE,
121 PCI_CB_IO_BASE_1, IORESOURCE_IO);
122
123 /* If I can enable prefetch for mem0 */
124 ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
125 ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
126 ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
127 ctl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
128 pci_write_config16(dev, PCI_CB_BRIDGE_CONTROL, ctl);
129 ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
130
131 /* See which bridge memory resources are implemented */
132 moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_0);
133 moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_0);
134 moving = moving_base & moving_limit;
135
136 /* Initialize the memory space constraints on the current bus */
137 type = IORESOURCE_MEM;
138 if (ctl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
139 type |= IORESOURCE_PREFETCH;
140 }
141 cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE,
142 PCI_CB_MEMORY_BASE_0, type);
143 if (type & IORESOURCE_PREFETCH) {
144 cardbus_size_bridge_resource(dev, PCI_CB_MEMORY_BASE_0);
145 }
146
147 /* See which bridge memory resources are implemented */
148 moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_1);
149 moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_1);
150 moving = moving_base & moving_limit;
151
152 /* Initialize the memory space constraints on the current bus */
153 cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE,
154 PCI_CB_MEMORY_BASE_1, IORESOURCE_MEM);
155 cardbus_size_bridge_resource(dev, PCI_CB_MEMORY_BASE_1);
156
157 compact_resources(dev);
158}
159
160void cardbus_enable_resources(device_t dev)
161{
162 uint16_t ctrl;
163 ctrl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
164 ctrl |= (dev->link[0].bridge_ctrl & (
165 PCI_BRIDGE_CTL_PARITY |
166 PCI_BRIDGE_CTL_SERR |
167 PCI_BRIDGE_CTL_NO_ISA |
168 PCI_BRIDGE_CTL_VGA |
169 PCI_BRIDGE_CTL_MASTER_ABORT |
170 PCI_BRIDGE_CTL_BUS_RESET));
171 ctrl |= (PCI_CB_BRIDGE_CTL_PARITY + PCI_CB_BRIDGE_CTL_SERR); /* error check */
172 printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
173 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
174
175 pci_dev_enable_resources(dev);
176
177 enable_childrens_resources(dev);
178}
179
180unsigned int cardbus_scan_bus(struct bus *bus,
181 unsigned min_devfn, unsigned max_devfn,
182 unsigned int max)
183{
184 return pci_scan_bus(bus, min_devfn, max_devfn, max);
185}
186
187
188unsigned int cardbus_scan_bridge(device_t dev, unsigned int max)
189{
190 struct bus *bus;
191 uint32_t buses;
192 uint16_t cr;
193
194 printk_spew("%s for %s\n", __func__, dev_path(dev));
195
196 bus = &dev->link[0];
197 bus->dev = dev;
198 dev->links = 1;
199
200 /* Set up the primary, secondary and subordinate bus numbers. We have
201 * no idea how many buses are behind this bridge yet, so we set the
202 * subordinate bus number to 0xff for the moment.
203 */
204 bus->secondary = ++max;
205 bus->subordinate = 0xff;
206
207 /* Clear all status bits and turn off memory, I/O and master enables. */
208 cr = pci_read_config16(dev, PCI_COMMAND);
209 pci_write_config16(dev, PCI_COMMAND, 0x0000);
210 pci_write_config16(dev, PCI_STATUS, 0xffff);
211
212 /*
213 * Read the existing primary/secondary/subordinate bus
214 * number configuration.
215 */
216 buses = pci_read_config32(dev, PCI_CB_PRIMARY_BUS);
217
218 /* Configure the bus numbers for this bridge: the configuration
219 * transactions will not be propagated by the bridge if it is not
220 * correctly configured.
221 */
222 buses &= 0xff000000;
223 buses |= (((unsigned int) (dev->bus->secondary) << 0) |
224 ((unsigned int) (bus->secondary) << 8) |
225 ((unsigned int) (bus->subordinate) << 16));
226 pci_write_config32(dev, PCI_CB_PRIMARY_BUS, buses);
227
228 /* Now we can scan all subordinate buses
229 * i.e. the bus behind the bridge.
230 */
231 max = cardbus_scan_bus(bus, 0x00, 0xff, max);
232
233 /* We know the number of buses behind this bridge. Set the subordinate
234 * bus number to its real value.
235 */
236 bus->subordinate = max;
237 buses = (buses & 0xff00ffff) |
238 ((unsigned int) (bus->subordinate) << 16);
239 pci_write_config32(dev, PCI_CB_PRIMARY_BUS, buses);
240 pci_write_config16(dev, PCI_COMMAND, cr);
241
242 printk_spew("%s returns max %d\n", __func__, max);
243 return max;
244}
245
246struct device_operations default_cardbus_ops_bus = {
247 .read_resources = cardbus_read_resources,
248 .set_resources = pci_dev_set_resources,
249 .enable_resources = cardbus_enable_resources,
250 .init = 0,
251 .scan_bus = cardbus_scan_bridge,
252 .enable = 0,
253 .reset_bus = pci_bus_reset,
254};