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